/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 * PhoneBookStubsToOthers.c
 *
 * Project:
 * --------
 *   MAUI
 *
 * Description:
 * ------------
 *   PhoneBook APIs for other applications
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/
#include "MMI_include.h"
#ifndef _PHONEBOOKSTUBSTOOTHERS_C
#define _PHONEBOOKSTUBSTOOTHERS_C

#include "ProtocolEvents.h"
#include "PhonebookDef.h"
#include "PhoneBookTypes.h"
#include "PhonebookProt.h"
#include "IdleAppDef.h"
#include "IdleAppProt.h"
#include "SpeedDial.h"
#include "CommonScreens.h"
#include "SimDetectionGprot.h"
#include "CallManagementGProt.h"        /* For isInCall */
#include "ProfileGprots.h"              /* For IsSilentModeActivated */
#include "AllAppGprot.h"
#include "wgui_inputs.h"
#include "GSM7BitDefaultAlphabet.h"
#include "gpioInc.h"
#include "App_usedetails.h"
#include "custom_ecc.h"

#if defined(MMS_SUPPORT)        /* For MMS */
#include "wap_ps_struct.h"
#include "Mmsadp.h"				/* mms_address_insert_hdlr */
#endif /* defined(MMS_SUPPORT) */ 

#if defined(__MOD_SMSAL__)
#include "SMSApi.h"
#include "MessagesExDcl.h"
#include "MessagesResourceData.h"
#endif /* defined(__MOD_SMSAL__) */ 

#if defined(__MMI_FILE_MANAGER__)       /* Also for __MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__ */
#include "FileManagerGProt.h"
#include "FileMgr.h"
#endif /* defined(__MMI_FILE_MANAGER__) */ 

#include "SettingDefs.h"
#include "SettingsGdcl.h"
#include "SettingGenum.h"
#include "SecuritySetup.h"
#include "SettingGprots.h"

#if defined(__MMI_EMAIL__)
#include "EmailAppGProt.h"      /* Email App List Screen */
#endif 

#if defined(__MMI_VRSD_DIAL__)
#include "mdi_datatype.h"
#include "mdi_audio.h"
#include "VRSD.h"
#include "VRSDResDef.h"
#include "VRSDDial.h"   /* Voice dial application list screen */
#include "MainMenuDef.h"
#endif /* defined(__MMI_VRSD_DIAL__) */ 

#if defined(__MMI_VRSI__)
#include "Mdi_datatype.h"
#include "VRSIResDef.h"
#include "VRSIType.h"
#include "VRSIProt.h"
#include "MainMenuDef.h"
#endif /* defined(__MMI_VRSI__) */ 

#if defined(MMS_SUPPORT)
#include "Wapadp.h"
#endif 
#ifdef __MMI_PHB_BIRTHDAY_FIELD__
#include "app_datetime.h"
#endif
#ifdef __J2ME__
#include "j2me_custom_option.h"
#endif

#ifdef __SYNCML_SUPPORT__
#include "vObjects.h"
#include "vCard.h"
#include "SyncMLDef.h"
#include "SyncMLGprot.h"
#endif /* __SYNCML_SUPPORT__ */

#ifdef __MMI_UCM__
#include "UCMGprot.h"
#endif /* __MMI_UCM__*/

#ifdef __MMI_DUAL_SIM_MASTER__
#include "MTPNP_PFAL_Master_ADN.h"
#include "MTPNP_AD_resdef.h"
#endif	/* __MMI_DUAL_SIM_MASTER__ */

#ifdef __MMI_URI_AGENT__
#include "URIAgentGprot.h"
#include "Conversions.h"
#endif

#ifdef __MMI_PHB_SAVE_CONTACT_NOTIFY__
#include "SecuritySetup.h"
#endif

#ifdef __MMI_VCARD__
#include "vcard.h"
#endif

#ifdef __MMI_UDX_VCARD_SUPPORT__
#include "UDXGprot.h"
#endif

/*
 * Define
 */
#if defined(__MMI_PHB_SAVE_CONTACT_NOTIFY__)
#define MMI_PHB_MAX_UNSAVED_DATA_NUM 5
#endif /* defined(__MMI_PHB_SAVE_CONTACT_NOTIFY__) */

/*
 * Local Variable
 */
U16 phb_list_view_type;
static mmi_phb_get_data_callback_type phb_list_view_call_back;
static mmi_phb_get_index_callback_type phb_list_view_get_index_call_back;
#if defined(__MMI_MESSAGES_SEND_BY_GROUP__) || defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__) || defined(__MMI_JATAAYU_MMS_ADD_NUMBER_BY_GROUP__)
static mmi_phb_get_index_from_group_callback_type phb_group_view_call_back;
#endif
MMI_PHB_LIST_VIEW phbListView;
U8 g_phb_enter_from;                            /* Identify Save number from idle screen */
FuncPhbListCallback g_phb_list_callback = NULL; /* For generic list call back function */

#if defined(__MMI_PHB_SAVE_CONTACT_NOTIFY__)
static U8 g_phb_unsaved_data_num = 0;
static U8 g_phb_unsaved_data_index;
static U8 g_phb_unsaved_data_op_state;
struct mmi_phb_unsaved_data_node_struct *unsaved_data_head = (struct mmi_phb_unsaved_data_node_struct*) NULL;
struct mmi_phb_unsaved_data_node_struct *unsaved_data_end = (struct mmi_phb_unsaved_data_node_struct*) NULL;
static MMI_BOOL g_phb_is_first_time_confirm = MMI_FALSE;
#endif /* defined(__MMI_PHB_SAVE_CONTACT_NOTIFY__) */

/*
 * Global Variable
 */
extern U16 PhoneBookEntryCount;
extern MMI_PHB_ENTRY_BCD_STRUCT PhoneBook[];

#ifdef __MMI_PHB_USIM_FIELD__
extern MMI_PHB_EMAIL_STRUCT phb_email[MAX_PB_SIM_ENTRIES];
extern MMI_PHB_NUMBER_BCD_STRUCT phb_anr[MAX_PB_SIM_ENTRIES][3];
#endif /* __MMI_PHB_USIM_FIELD__ */ 

#ifndef __MMI_PHB_NO_OPTIONAL_FIELD__
extern PHB_OPTIONAL_FIELDS_STRUCT PhoneBookOptionalFields;
#endif 

#if defined(__MMI_PHB_INFO_FIELD__)
extern PHB_INFO_FIELDS_STRUCT PhoneBookInfoFields;
#endif

#if defined(__MMI_PHB_GEOGRAPHIC_FIELD__)
extern MMI_PHB_GEOGRAPHIC_FIELD_STRUCT PhoneBookGeoFields;
#endif

extern S8 pbName[];
extern S8 pbNumber[];
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) || defined(__MMI_PHB_USIM_FIELD__)
extern S8 pbHomeNumber[];
extern S8 pbCompanyName[];
extern S8 pbEmailAddress[];
#if defined(__MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__)
extern S8 pbEmailAddress2[];
#endif
extern S8 pbOfficeNumber[];
extern S8 pbFaxNumber[];
#endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) || defined(__MMI_PHB_USIM_FIELD__) */

#ifdef __MMI_PHB_BIRTHDAY_FIELD__
extern const kal_uint8 g_dt_day_per_mon[];
extern S8 pbDay[];
extern S8 pbMon[];
extern S8 pbYear[];
#endif /* __MMI_PHB_BIRTHDAY_FIELD__ */

#if defined(__MMI_PHB_INFO_FIELD__)
extern S8 pbTitle[];
extern S8 pbUrl[];
extern S8 pbAdrPOBox[];
extern S8 pbAdrExtension[];
extern S8 pbAdrStreet[];
extern S8 pbAdrCity[];
extern S8 pbAdrState[];
extern S8 pbAdrPostalCode[];
extern S8 pbAdrCountry[];
extern S8 pbNote[];
#endif /* defined(__MMI_PHB_INFO_FIELD__) */ 

#if defined(__MMI_VOIP__)
extern S8 pbVoip[(VOIP_URI_LEN) * ENCODING_LENGTH];
#endif

#ifdef __MMI_PHB_USIM_FIELD__
extern S8 pbNickName[];
#endif /* __MMI_PHB_USIM_FIELD__ */ 

#if defined(__MMI_FILE_MANAGER__)
extern S8 g_phb_file_path[];    /* Select file from file manager */
#ifdef __MTK_TARGET__
__align(2) static S8 phb_caller_pic_file_path[MMI_PHB_MAX_PATH_LENGTH];
#else
static S8 phb_caller_pic_file_path[MMI_PHB_MAX_PATH_LENGTH];
#endif
#endif /* defined(__MMI_FILE_MANAGER__) */ 

extern U16 g_phb_image_type_id;
#ifdef __MMI_PHB_SIM_NUMBER_MANAGEMENT__
extern MMI_PHB_OP_ENUM g_phb_add_more_op;
#endif

extern U16 g_phb_name_index[];
extern U16 TitleStrings[];

#if defined(__MMI_INCOMING_CALL_VIDEO__)
extern S8 g_phb_video_file_path[];  /* Select video file from file manager */
#endif 

#if defined(__MMI_INCOMING_CALL_VIDEO__)
#if defined(__MMI_SWFLASH__) && defined(__MMI_AVATAR__)
extern U8 g_phb_swflash_video_minus;
extern U8 g_phb_avatar_video_minus;
#elif defined(__MMI_SWFLASH__) /* defined(__MMI_SWFLASH__) && defined(__MMI_AVATAR__) */
extern U8 g_phb_swflash_video_minus;
#elif defined(__MMI_AVATAR__)
extern U8 g_phb_avatar_video_minus;
#endif /* defined(__MMI_SWFLASH__) && defined(__MMI_AVATAR__) */
#endif 

#if defined(__MMI_PHB_GENERIC_MULTI_SELECT__)
extern U16 g_phb_multi_select_limit;
#endif /* defined(__MMI_PHB_GENERIC_MULTI_SELECT__) */

/* for general save contact option */
extern U8 g_phb_nlf_list_storage;
U8 g_phb_save_contact_opt_storage;
MMI_PHB_FIELDS_STRUCT* g_phb_save_contact_data = NULL;

/*
 * Local Function
 */
#if defined(__MMI_PHB_GENERIC_MULTI_SELECT__)
mmi_phb_get_index_from_group_callback_type phb_generic_multi_select_list_call_back;
mmi_phb_get_number_type_callback_type phb_mark_several_list_call_back;
#if defined(__MMI_MESSAGES_SEND_BY_GROUP__) || defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__) || defined(__MMI_JATAAYU_MMS_ADD_NUMBER_BY_GROUP__)
mmi_phb_get_number_type_callback_type phb_mark_several_group_call_back;
#endif
#endif /* defined(__MMI_PHB_GENERIC_MULTI_SELECT__) */

#if defined(MBM_IMIBASE_SUPPORT)

extern void mmi_phb_list_entry_for_number_and_email(void);
extern void mbm_phb_pre_send_data();

void mbm_pb_list_pre_entry_for_number_and_email()
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phb_list_view_type = MMI_PHB_ENTRY_FIELD_ALL_NUMBER;
    phbListView = MMI_PHB_LIST_FOR_OTHER_APP;
    mmi_phb_list_entry_for_number_and_email();
}
#endif //MBM_IMIBASE_SUPPORT

/*
 * Global Function
 */
extern void SendPhbEntryForDivert(S8* name, S8* number);
extern void CBackCallDeflectNumberFromPB(PS8 CallDeflectBuffer);

extern void mmi_msg_send_msg_to_group(U16, U16 *);
extern void mmi_frm_sms_go_back_from_send(void);
extern void mmi_frm_sms_end_key_from_send(void);
extern MMI_PHB_ENTRY_STRUCT *mmi_phb_get_sim_entry(void);
#ifdef __MMI_DUAL_SIM_MASTER__
extern MMI_PHB_ENTRY_STRUCT *mmi_phb_get_slave_sim_entry(void);
#endif /* __MMI_DUAL_SIM_MASTER__*/

#ifdef __J2ME__
extern void mmiapi_fullscreen_editor_set_number(S8* name, S8* number);
#endif

#if defined(__MMI_VRSI__) && defined(__MMI_VRSI_TRAIN_TAG__)
static vrsi_sd_tag_struct* g_phb_vrsi_tag_list;
#endif

#if defined (JATAAYU_SUPPORT)
extern void update_mms_number_from_phonebook(S8 *number);
extern void update_mms_email_from_phonebook(S8 *email);
#endif /* JATAAYU_SUPPORT */

#if defined(OBIGO_Q03C_MMS_V01)
extern void widget_MMI_fullscreen_editor_update_from_phonebook(kal_uint8 *data);
#endif /* defined(OBIGO_Q03C_MMS_V01) */

mmi_phb_save_entry_from_others_callback_type mmi_phb_add_new_entry_callback;

#if defined(__MMI_PHB_GENERIC_MULTI_SELECT__)
extern mmi_phb_check_selected_index_callback_type phb_generic_multi_select_check_call_back;
extern mmi_phb_check_number_type_callback_type phb_mark_several_check_call_back;
#endif /* defined(__MMI_PHB_GENERIC_MULTI_SELECT__) */

#if defined(__MMI_IMPS__) && defined(IMPS_WV13_ENHANCEMENT) && defined(__MMI_VCARD__)
extern mmi_vcard_send_by_imps_cbk_type g_vcard_send_by_imps_cb;
#endif

#ifdef __MMI_URI_AGENT__
mmi_uriagent_appid_enum mmi_phb_uriagent_ura_appid;
mmi_uriagent_uri_request_hdlr_cb mmi_phb_uriagent_callback;
extern int mmi_charset_utf8_to_ucs2_length_in_bytes(const kal_uint8 *raw);
#endif

/*****************************************************************************
 * FUNCTION
 *  lookUpName
 * DESCRIPTION
 *  Search for the number entry for corresponding name input
 * PARAMETERS
 *  name        [IN]        Input name to be serach
 * RETURNS
 *  result number string if found
 *****************************************************************************/
PS8 lookUpName(PS8 name)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;
    U16 store_index;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (i = 0; i < PhoneBookEntryCount; i++)
    {
        store_index = g_phb_name_index[i];

        if (mmi_ucs2cmp((PS8) name, (PS8) PhoneBook[store_index].alpha_id.name) == 0)
        {
            mmi_phb_convert_get_ucs2_number(pbNumber, i);   /* BCD number format. */
            return (S8*) pbNumber;
        }
    }
    return NULL;
}


/*****************************************************************************
 * FUNCTION
 *  lookUpNumber
 * DESCRIPTION
 *  Search for the name entry for corresponding number input
 * PARAMETERS
 *  number      [IN]        Input number to be serach
 * RETURNS
 *  result name string if found
 *****************************************************************************/
PS8 lookUpNumber(PS8 number)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i = 0xffff;
    U8 flag = 0;
    U32 num;
    U8 number_ASCII[MAX_PB_NUMBER_LENGTH + 1 + 1];
    U8 number_UCS2[(MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_LOOK_UP_NUMBER);

    memset(number_ASCII, 0, (MAX_PB_NUMBER_LENGTH + 1 + 1));
    memset(number_UCS2, 0, (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
    
    mmi_phb_truncate_number_field_to_max_len(number_UCS2, (U8*) number);

    mmi_ucs2_n_to_asc((PS8) number_ASCII, (PS8) number_UCS2, (MAX_PB_NUMBER_LENGTH + 1));

    MMI_PRINT(MOD_MMI_COMMON_APP, MMI_PHB_TRACE_GROUP,"[PHB] lookUpNumber(),  number_ASCII:%s", number_ASCII);

    /* Lookup into table when (1) lookup table not empty (2)Not Processing (3)All Entries populated and sorted */
    if (g_phb_cntx.lookup_table_count && !g_phb_cntx.processing && (g_phb_cntx.populate_count == 0xffff)
    #ifdef __SYNCML_SUPPORT__
      && !(mmi_syncml_is_phb_sync_now())
    #endif /* __SYNCML_SUPPORT__ */
    )
    {
        num = mmi_phb_util_convert_number_to_int(number_ASCII);
        if (num < INVALID_NUMBER)
        {
            i = mmi_phb_lookup_table_search(num, 0, (U16) (g_phb_cntx.lookup_table_count - 1), 
                (S8*) number_ASCII, MMI_STORAGE_BOTH);

            if (i < 0xffff) /* i is storage location in array, begin from 0. */
            {
                flag = 1;
            }
        }
    }

    if (flag)
    {
        return (PS8) PhoneBook[i].alpha_id.name;
    }
    else
    {
        return NULL;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_truncate_number_field_to_max_len
 * DESCRIPTION
 *  save the last MAX_PB_NUMBER_LENGTH digit in the number field
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_truncate_number_field_to_max_len(U8* ucs2_num_dest, U8* ucs2_num_src)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 num_len;
    U8 num_plus_index = 0;
    U16 i;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    memset(ucs2_num_dest, 0, (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);

    num_len = mmi_ucs2strlen((S8*) ucs2_num_src);
    if (num_len > MAX_PB_NUMBER_LENGTH)
    {
        if (ucs2_num_src[0] == '+')
        {
            ucs2_num_dest[0] = ucs2_num_src[0];
            num_plus_index++;
        }

        for (i = 0; i < MAX_PB_NUMBER_LENGTH; i++)
        {
            ucs2_num_dest[2 * (i + num_plus_index)] = ucs2_num_src[2 * (num_len - MAX_PB_NUMBER_LENGTH - 1 + i + num_plus_index)];
            ucs2_num_dest[2 * (i + num_plus_index) + 1] = ucs2_num_src[2 * (num_len - MAX_PB_NUMBER_LENGTH - 1 + i + num_plus_index) + 1];
        }
    }
    else
    {
        mmi_ucs2ncpy((S8*) ucs2_num_dest, (S8*) ucs2_num_src, MAX_PB_NUMBER_LENGTH);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_generic_enter_list
 * DESCRIPTION
 *  Phonebook list interface for other application.
 * PARAMETERS
 *  func_list       [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_generic_enter_list(FuncPhbListCallback func_list)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phbListView = MMI_PHB_LIST_FOR_ALL_SHARED;
    g_phb_list_callback = func_list;
    mmi_phb_list_pre_entry_second_level();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_generic_enter_list_result
 * DESCRIPTION
 *  choose entry result, send result back to caller app.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_generic_enter_list_result(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
    S16 pError;
#endif
    U16 store_index;
    MMI_PHB_VCARD_STRUCT *result_entry = OslMalloc(sizeof(MMI_PHB_VCARD_STRUCT));
    S8 tempNumber[MAX_PB_NUMBER_LENGTH + 1 + 1];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* Clear buffer */
    memset(result_entry, 0, sizeof(MMI_PHB_VCARD_STRUCT));

    store_index = g_phb_name_index[g_phb_cntx.active_index_second];

    /* Copy Name */
    mmi_ucs2cpy(result_entry->name, (S8*) PhoneBook[store_index].alpha_id.name);

    /* Copy Number - ASCII Type */
    if (((PhoneBook[store_index].tel.type & 0x90) == 0x90) && (PhoneBook[store_index].tel.type != 0xFF))
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, PhoneBook[store_index].tel.number, MAX_PB_NUMBER_LENGTH + 1, store_index);
        /* now number is +**21*8123456# */
        /* call the third API to transfer + to place just after SS pattern */
        applib_decode_number((U8*)tempNumber, (U8*)result_entry->number, 0x91);
        /* now number is **21*+8123456#, so we can show the original data to user */
    }
    else
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) result_entry->number, PhoneBook[store_index].tel.number, MAX_PB_NUMBER_LENGTH + 1, store_index);
    }

#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
    if (store_index < MAX_PB_PHONE_ENTRIES) /* in NVRAM */
    {
        ReadRecord(
            NVRAM_EF_PHB_FIELDS_LID,
            (U16) (store_index + 1),
            (void*)&PhoneBookOptionalFields,
            OPTIONAL_FIELDS_RECORD_SIZE,
            &pError);

        strcpy(result_entry->homeNumber, (PS8) PhoneBookOptionalFields.homeNumber);
        strcpy(result_entry->officeNumber, (PS8) PhoneBookOptionalFields.officeNumber);
        strcpy(result_entry->faxNumber, (PS8) PhoneBookOptionalFields.faxNumber);
        strcpy(result_entry->emailAddress, (S8*) PhoneBookOptionalFields.emailAddress);  
    #ifdef __MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__
        strcpy(result_entry->emailAddress2, (S8*) PhoneBookOptionalFields.emailAddress2);  
    #endif
        mmi_ucs2cpy(result_entry->companyName, (PS8) PhoneBookOptionalFields.companyName);
    }
#endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 

    /* Send back to email app. */
    if (g_phb_list_callback != NULL)
    {
        g_phb_list_callback(result_entry);
    }

    OslMfree(result_entry);
    g_phb_list_callback = NULL;
    DeleteScreenIfPresent(SCR_PBOOK_LIST);
}


#define MMI_PHB_IDLE_SCREEN
#if (defined(__MMI_KEYPAD_LOCK_PATTERN_1__) && !defined(__MMI_DISABLE_KEYPAD_LOCK__))
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_idle_enter_keypad_lock_callback
 * DESCRIPTION
 *  register '#' to again
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_phb_idle_enter_keypad_lock_callback(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    IdleHandleKeypadLockProcess();
#ifdef __MMI_PHB_QUICK_SEARCH__
    if (mmi_imc_is_connected())
    {
        mmi_imc_key_register_ime_keys();
    }
#endif
}
#endif /* (defined(__MMI_KEYPAD_LOCK_PATTERN_1__) && !defined(__MMI_DISABLE_KEYPAD_LOCK__)) */


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_idle_enter_phb_list
 * DESCRIPTION
 *  Displays PHB list from RSK of idle screen
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_idle_enter_phb_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_idle_context.ToNameScrFromIdleApp = 1;
    g_idle_context.RskPressedFromIdleApp = 1;
    phbListView = MMI_PHB_LIST_FOR_PHB;
    g_phb_cntx.highlight_entry = 0;

    g_phb_cntx.refresh_list = MMI_PHB_ENTRY_REFRESH;

#if defined(__MMI_OP11_HOMESCREEN__)
/* under construction !*/
#else 
    mmi_phb_entry_main_menu();
#endif

#if (defined(__MMI_KEYPAD_LOCK_PATTERN_1__) && !defined(__MMI_DISABLE_KEYPAD_LOCK__))
    if (g_idle_context.RskPressedFromIdleApp == 1)
    {
        StartTimer(KEYPAD_LOCK_TIMER, KEYPAD_LOCK_TIMEOUT, mmi_phb_idle_enter_keypad_lock_callback);
        SetKeyHandler(IdleHandlePoundKeyForKeypadLock, KEY_POUND, KEY_EVENT_UP);
    }
#endif /* (defined(__MMI_KEYPAD_LOCK_PATTERN_1__) && !defined(__MMI_DISABLE_KEYPAD_LOCK__)) */ 
}


/*****************************************************************************
 * FUNCTION
 *  SaveNumberFromIdleScrn
 * DESCRIPTION
 *  Saves number from idle screen
 * PARAMETERS
 *  number          [IN]        Number string to be saved
 *  enter           [IN]        Enter location
 * RETURNS
 *  void
 *****************************************************************************/
void SaveNumberFromIdleScrn(PS8 number, U8 enter)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_SAVE_NUMBER_FROM_IDLE_SCREEN);
    /* Check number length first */
    if (mmi_ucs2strlen((PS8) number) > MAX_PB_NUMBER_LENGTH + 1)
    {
        mmi_display_popup((UI_string_type)GetString(STR_NUMBER_TOO_LONG), MMI_EVENT_FAILURE);
        return;
    }
    /* Avoid add entry while copy all and delete all */
    if (mmi_phb_check_ready(MMI_TRUE))
    {
    #if defined(__MMI_SAVE_CONTACT_REPLACE__)
        MMI_PHB_FIELDS_STRUCT *phb_data;
        phb_data = OslMalloc(sizeof(MMI_PHB_FIELDS_STRUCT));
        memset(phb_data, 0x00, sizeof(MMI_PHB_FIELDS_STRUCT));

        phb_data->number = OslMalloc((MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
        memset(phb_data->number, 0x00, (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(phb_data->number, (S8*) number, MAX_PB_NUMBER_LENGTH + 1);

        g_phb_cntx.set_done_flag = 1;
        mmi_phb_clear_old_history();
        mmi_phb_save_contact_from_others2(enter, g_phb_cntx.prefer_storage, phb_data);
        OslMfree(phb_data->number);
        OslMfree(phb_data);
    #else /* __MMI_SAVE_CONTACT_REPLACE__ */
        g_phb_cntx.set_done_flag = 1;
        mmi_ucs2cpy(pbNumber, number);

        g_phb_enter_from = enter;
        mmi_phb_clear_old_history();
        mmi_phb_op_add_pre_entry();
    #endif /* __MMI_SAVE_CONTACT_REPLACE__ */
    }
}


#ifdef __MMI_PHB_BIRTHDAY_FIELD__
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_retrieve_bday
 * DESCRIPTION
 *  parse birthday field of a vcard object.s
 * PARAMETERS
 *  b_string        [?]         
 *  pb_year         [?]         
 *  pb_mon          [?]         
 *  pb_day          [?]         
 *  change_flag     [IN]        
 *  String(?)       [IN]        String read from file for one line
 * RETURNS
 *  parse error cause(?)
 *****************************************************************************/
void mmi_phb_retrieve_bday(S8 *b_string, S8 *pb_year, S8 *pb_mon, S8 *pb_day, BOOL change_flag)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S8 temp_buf[10 + 1]; /* 19990101 or 1999-01-01 */
    S8 ascii_buff[6];

    U16 b_year;
    U8 b_month;
    U8 b_day;
    U8 is_valid = TRUE;
    U8 i;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* b_string; */
    memset(temp_buf, 0, sizeof(temp_buf));
    strncpy(temp_buf, b_string, 10);

    /* Year */
    strncpy(ascii_buff, temp_buf, 4);
    ascii_buff[4] = '\0';
    mmi_asc_to_ucs2(pb_year, ascii_buff);
    b_year = (U16) gui_atoi((U16*) pb_year);
    i = 4;
    /* Month */
    if (*(temp_buf + i) == '-')
    {
        i++;
    }
    strncpy(ascii_buff, temp_buf + i, 2);
    ascii_buff[2] = '\0';
    mmi_asc_to_ucs2(pb_mon, ascii_buff);
    b_month = (U8) gui_atoi((U16*) pb_mon);
    i += 2;
    /* Day */
    if (*(temp_buf + i) == '-')
    {
        i++;
    }    
    strncpy(ascii_buff, temp_buf + i, 2);
    ascii_buff[2] = '\0';
    mmi_asc_to_ucs2(pb_day, ascii_buff);
    b_day = (U8) gui_atoi((U16*) pb_day);

    if (b_year > MMI_PHB_BDAY_MAX_YEAR_INT || b_year < 1900)
    {
        is_valid = FALSE;
    }
    else if (b_month > 12 || b_month < 1)
    {
        is_valid = FALSE;
    }

    if (b_month == 2)
    {
        if (b_day > (g_dt_day_per_mon[b_month - 1] + applib_dt_is_leap_year(b_year)) || b_day < 1)
            is_valid = FALSE;
    }
    else if (b_day > g_dt_day_per_mon[b_month - 1] || b_day < 1)
    {
        is_valid = FALSE;
    }

    if (change_flag)
    {
        if (is_valid)
        {
            mmi_phb_bday_set_changed();
        }
        else
        {
            mmi_phb_util_clear_bday_buffer();
        }
    }
}
#endif /* __MMI_PHB_BIRTHDAY_FIELD__ */ 


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_save_entry_from_others
 * DESCRIPTION
 *  Saves phonebook entry from other applications
 * PARAMETERS
 *  ucs2_name       [IN]        Name string to be saved. Max length is MAX_PB_NAME_LENGTH
 *  number          [IN]        Number string to be saved(UCS2), if the number lenght = 41, the first char must be '+'
 *  opt_fields      [IN]        Optional fields
 *  b_day           [IN]         Birthday to be saved. Format: yyyymmdd. ex: 19750101
 *  info_fields     [IN]        Information fields
 *  nick_name       [IN]        Nick name string to be saved(UCS2). Max length is MAX_PB_NAME_LENGTH
 *  phb_callback    [IN]        callback function after saving  
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_save_entry_from_others(
        S8 *ucs2_name,
        S8 *number,
        PHB_OPTIONAL_FIELDS_STRUCT *opt_fields,
        S8 *b_day,
        PHB_INFO_FIELDS_STRUCT *info_fields,
        S8 *nick_name,
        mmi_phb_save_entry_from_others_callback_type phb_callback)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_SAVE_ENTRY_FROM_OTHERS);

    /* Check number length first */
    if ((mmi_ucs2strlen(number) > MAX_PB_NUMBER_LENGTH + 1))
    {
        mmi_display_popup((UI_string_type)GetString(STR_NUMBER_TOO_LONG), MMI_EVENT_FAILURE);
        return;
    }
    /* Avoid add entry while copy all and delete all */
    if (mmi_phb_check_ready(MMI_TRUE))
    {
        g_phb_cntx.set_done_flag = 1;
        mmi_phb_clear_old_history();
        mmi_phb_util_clear_buffer(TRUE);

        if (ucs2_name)
        {        	
            mmi_ucs2ncpy(pbName, ucs2_name, MAX_PB_NAME_LENGTH);        	
            mmi_phb_truncate_pbname_buffer(MMI_NVRAM);
        }
        if (number)
        {
            mmi_ucs2ncpy(pbNumber, number, MAX_PB_NUMBER_LENGTH + 1);
        }
    #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) || defined(__MMI_PHB_USIM_FIELD__)
        if (opt_fields)
        {
            mmi_asc_to_ucs2(pbHomeNumber, (PS8) opt_fields->homeNumber);
            mmi_asc_to_ucs2(pbEmailAddress, (PS8) opt_fields->emailAddress);
        #ifdef __MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__
            mmi_asc_to_ucs2(pbEmailAddress2, (PS8) opt_fields->emailAddress2);
        #endif
            mmi_asc_to_ucs2(pbOfficeNumber, (PS8) opt_fields->officeNumber);
            mmi_ucs2cpy(pbCompanyName, (PS8) opt_fields->companyName);
            mmi_asc_to_ucs2(pbFaxNumber, (PS8) opt_fields->faxNumber);
        }
    #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) || defined(__MMI_PHB_USIM_FIELD__) */        
    #ifdef __MMI_PHB_BIRTHDAY_FIELD__
        if (b_day)
        {
            mmi_phb_retrieve_bday(b_day, (S8*) pbYear, (S8*) pbMon, (S8*) pbDay, TRUE);
        }
    #endif /* __MMI_PHB_BIRTHDAY_FIELD__ */ 
    #ifdef __MMI_PHB_INFO_FIELD__
        if (info_fields)
        {
            mmi_ucs2cpy(pbTitle, (PS8) info_fields->title);
            mmi_asc_to_ucs2(pbUrl, (PS8) info_fields->url);
            mmi_ucs2cpy(pbAdrPOBox, (PS8) info_fields->address.pobox);
            mmi_ucs2cpy(pbAdrExtension, (PS8) info_fields->address.extension);
            mmi_ucs2cpy(pbAdrStreet, (PS8) info_fields->address.street);
            mmi_ucs2cpy(pbAdrCity, (PS8) info_fields->address.city);
            mmi_ucs2cpy(pbAdrState, (PS8) info_fields->address.state);
            mmi_ucs2cpy(pbAdrPostalCode, (PS8) info_fields->address.postalcode);
            mmi_ucs2cpy(pbAdrCountry, (PS8) info_fields->address.country);
            mmi_ucs2cpy(pbNote, (PS8) info_fields->note);
        }
    #endif /* __MMI_PHB_INFO_FIELD__ */ 
    #ifdef __MMI_PHB_USIM_FIELD__
        if (nick_name)
        {
            mmi_ucs2cpy(pbNickName, (PS8) info_fields->note);
        }
    #endif /* __MMI_PHB_USIM_FIELD__ */ 
        g_phb_enter_from = MMI_PHB_ENTER_FROM_OTHERS;
        mmi_phb_add_new_entry_callback = phb_callback;

        /* save to prefer storage directly, if storage full, entry choose storage screen */
        mmi_phb_op_add_pre_entry_to_prefer_storage();
    }
}


#ifdef __MMI_VOIP__
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_call_get_data_for_voip
 * DESCRIPTION
 *  Returns the names/ringtone/image tag/caller group details for the incoming
 *  call number if it is present in PHB
 * PARAMETERS
 *  uri         [IN]        uri string to be matched
 *  is_mo_call  [IN]        dial_from_list only for mo call
 * RETURNS
 *  PHB_CM_INTERFACE  a interface structure
 *****************************************************************************/
PHB_VOIP_INTERFACE mmi_phb_call_get_data_for_voip(PU8 uri, BOOL is_mo_call)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index = 0xffff, grp_id;
    PHB_VOIP_INTERFACE phbDetails;
    U8 flag = 0;
    U8 uri_ASCII[VOIP_URI_LEN];
    U8 *uri_ptr;
    PHB_OPTIONAL_IDS_STRUCT *opt_ids = NULL;
    PHB_CALLER_GROUP_STRUCT *callerGroups;

#if defined(__MMI_INCOMING_CALL_VIDEO__)
    U16 video_id;
#endif 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    memset(uri_ASCII, 0, ENCODING_LENGTH);
    memset((void*)&phbDetails, 0, sizeof(phbDetails));

    phbDetails.pictureId = IMG_PHB_DEFAULT;
    phbDetails.dialInList = MMI_PHB_NONE;
    mmi_ucs2_to_asc((PS8) uri_ASCII, (PS8) uri);

    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_CALL_GET_DATA_FOR_VOIP, is_mo_call);
    MMI_PRINT(MOD_MMI_COMMON_APP, MMI_PHB_TRACE_GROUP,"[PHB] mmi_phb_call_get_data_for_voip(),  uri:%s", uri);

    uri_ptr = uri_ASCII;

    /* Check if dial out from phonebook list */
    if (g_phb_cntx.dial_from_list == MMI_PHB_PHONEBOOK && is_mo_call)
    {
        g_phb_cntx.dial_from_list = MMI_PHB_NONE;
        flag = 1;
        store_index = g_phb_name_index[g_phb_cntx.active_index];
        phbDetails.dialInList = MMI_PHB_PHONEBOOK;
    }
    /* For example, from birthday */
    else if ((g_phb_cntx.dial_from_list == MMI_PHB_FROM_OTHER_APP) && is_mo_call
        && !g_phb_cntx.processing
    #ifdef __SYNCML_SUPPORT__
        && !(mmi_syncml_is_phb_sync_now())
    #endif /* __SYNCML_SUPPORT__ */
        )
    {
        g_phb_cntx.dial_from_list = MMI_PHB_NONE;
        flag = 1;
        store_index = g_phb_cntx.store_index;
        phbDetails.dialInList = MMI_PHB_PHONEBOOK;
    }

    /* Map Number with one enrtry. */
    if (flag)
    {
        mmi_ucs2cpy((PS8) phbDetails.name, (PS8) PhoneBook[store_index].alpha_id.name);
        phbDetails.name_dcs = PhoneBook[store_index].alpha_id.name_dcs;

        /* Caller Group Setup */
    #if !defined(__MMI_PHB_CALLERGROUP_IN_SIM__)    /* Associate caller group in SIM card entry */
        if (store_index < MAX_PB_PHONE_ENTRIES)
    #endif 
        {
            callerGroups = (PHB_CALLER_GROUP_STRUCT*) g_phb_cntx.caller_group;

            mmi_phb_read_optional_ids(store_index); /* i is store index */
            opt_ids = (PHB_OPTIONAL_IDS_STRUCT*) g_phb_cntx.optional_ids;

            grp_id = opt_ids->callerGroupID;
            if (grp_id)
            {
            #if defined(__MMI_CALLERGROUP_NO_ALERT__)
                phbDetails.alertType = MMI_ALERT_NONE;
            #else
                phbDetails.alertType = callerGroups[grp_id - 1].alertType;
            #endif
                phbDetails.backlightId = callerGroups[grp_id - 1].LEDPatternId;
                phbDetails.ringtoneId = callerGroups[grp_id - 1].ringToneID;
                if (callerGroups[grp_id - 1].pictureTagID == 1) /* Associate Picture is in the file system. */
                {
                #if defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__)
                    phbDetails.pictureId = 1;
                    phbDetails.record_index = 5000 + grp_id;
                #endif /* defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__) */ 
                }
                else if (callerGroups[grp_id - 1].pictureTagID != 0)   /* 0 means default image */
                {
                    phbDetails.pictureId = callerGroups[grp_id - 1].pictureTagID;
                }

            #if defined(__MMI_INCOMING_CALL_VIDEO__)
                if (callerGroups[grp_id - 1].VideoID)
                {
                    phbDetails.videoId = callerGroups[grp_id - 1].VideoID;
                    phbDetails.video_record_index = (U16) (MAX_PB_PHONE_ENTRIES + grp_id);      /* MAX_PB_PHONE_ENTRIES for group 1 and so on. */
                }
            #endif /* defined(__MMI_INCOMING_CALL_VIDEO__) */ 
            }
        }

        /* Personal Entry Setup, these will overwrite caller group setting. */
        if (store_index < MAX_PB_PHONE_ENTRIES)
        {
            /* Associate Pictures */
            if (opt_ids->pictureTagID == 1)
            {
            #if defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__)
                phbDetails.pictureId = 1;
                phbDetails.record_index = store_index + 1;  /* Record Must in NVRAM, so the record index is store_index + 1 */
            #endif /* defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__) */ 
            #if defined(__MMI_INCOMING_CALL_VIDEO__)
                phbDetails.videoId = 0;    /* If Image Selected, should not play caller group's video */
            #endif 
            }
            else if ((opt_ids->pictureTagID != IMG_PHB_DEFAULT) && (opt_ids->pictureTagID != 0x0000))
            {
                phbDetails.pictureId = opt_ids->pictureTagID;
            #if defined(__MMI_INCOMING_CALL_VIDEO__)
                phbDetails.videoId = 0; /* If Image Selected, should not play caller group's video */
            #endif 
            }

            /* Associate Ring Tone */
            if (opt_ids->ringToneID)
            {
                phbDetails.ringtoneId = opt_ids->ringToneID;
            }

            /* For Incoming Call Video */
        #if defined(__MMI_INCOMING_CALL_VIDEO__)
            if ((video_id = mmi_phb_video_get_id_by_index(store_index)) != 0)
            {
                phbDetails.videoId = video_id;
                phbDetails.video_record_index = store_index + 1;    /* Record Must in NVRAM, so the record index is store_index + 1 */
            }
        #endif /* defined(__MMI_INCOMING_CALL_VIDEO__) */ 
        }
    }
    return phbDetails;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_save_entry_for_service_field
 * DESCRIPTION
 *  Saves number from idle screen
 * PARAMETERS
 *  uri             [IN]        uri to be saved
 *  enter           [IN]        Enter location
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_save_entry_for_service_field(PS8 name, PS8 uri, U8 enter)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_SAVE_ENTRY_FOR_SERVICE_FIELD);

    /* Check uri length first */
    if (enter == MMI_PHB_ENTER_FROM_VOIP &&  mmi_ucs2strlen((PS8)uri) >= VOIP_URI_LEN)
    {
        mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_ERROR), MMI_EVENT_FAILURE);
        return;
    }
    /* Avoid add entry while copy all and delete all */
    if (!mmi_phb_check_ready(MMI_TRUE))
    {
        return;
    }
    else if (g_phb_cntx.phone_total == g_phb_cntx.phone_used)
    {
        mmi_display_popup((UI_string_type)GetString(STR_PHONE_FULL_MSG), MMI_EVENT_FAILURE);
    }
    else
    {
        g_phb_cntx.set_done_flag = 1;
        g_phb_enter_from = MMI_PHB_ENTER_FROM_VOIP;
        mmi_phb_clear_old_history();
        mmi_phb_util_clear_buffer(TRUE);
        if (name)
        {
            mmi_ucs2cpy(pbName, name);
            mmi_phb_truncate_pbname_buffer(MMI_NVRAM);
        }
        if (uri)
        {
            mmi_ucs2cpy(pbVoip, uri);
        }
        //mmi_phb_entry_op_add_choose_storage();
        g_phb_cntx.selected_storage = MMI_NVRAM;
    #ifdef __MMI_PHB_SIM_NUMBER_MANAGEMENT__
        g_phb_add_more_op = MMI_PHB_OP_NONE;
    #endif
        mmi_phb_entry_op_add_entry();
    }
}
#endif /* __MMI_VOIP__ */


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_get_sim_entry_by_location
 * DESCRIPTION
 *  Return the number to idle screen for quick access
 * PARAMETERS
 *  entryNum        [IN]        Location to be search
 * RETURNS
 *  U8
 *****************************************************************************/
U8 mmi_phb_get_sim_entry_by_location(U16 entryNum)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;
    U16 record_index;
    U8 hasEntry = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!mmi_phb_check_ready(MMI_TRUE))
    {
        return hasEntry;
    }
    else if (mmi_secset_is_fdn_enabled())
    {
        mmi_display_popup((UI_string_type)GetString(STR_FDL_IS_ON), MMI_EVENT_FAILURE);
    }
    else if (entryNum == 0 || entryNum > g_phb_cntx.sim_total || !mmi_bootup_is_sim_valid())
    {
        mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_INVALID_LOCATION), MMI_EVENT_FAILURE);
    }
    else
    {
        for (i = 0; i < PhoneBookEntryCount; i++)
        {
            record_index = g_phb_name_index[i] - MAX_PB_PHONE_ENTRIES + 1;
            if (record_index == entryNum)
            {
                g_phb_cntx.active_index = i;
                hasEntry = 1;
                mmi_phb_entry_op_view_entry();
            }
        }

        if (!hasEntry)
        {
            mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_NO_CONTACT), MMI_EVENT_FAILURE);
        }
    }

    return hasEntry;
}


#define MMI_PHB_INTERFACE_FOR_CALL
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_call_get_data_for_call_mgnt
 * DESCRIPTION
 *  Returns the names/ringtone/image tag/caller group details for the incoming
 *  call number if it is present in PHB
 * PARAMETERS
 *  number      [IN]        Number string to be matched
 *  is_mo_call  [IN]        dial_from_list only for mo call
 * RETURNS
 *  PHB_CM_INTERFACE  a interface structure
 *****************************************************************************/
#ifdef __MMI_DUAL_SIM_MASTER__
PHB_CM_INTERFACE mmi_phb_call_get_data_for_call_mgnt(PU8 number, BOOL is_mo_call, U8 simInterface)
#else /* __MMI_DUAL_SIM_MASTER__ */
PHB_CM_INTERFACE mmi_phb_call_get_data_for_call_mgnt(PU8 number, BOOL is_mo_call)
#endif /* __MMI_DUAL_SIM_MASTER__ */
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index = 0xffff, grp_id;
    PHB_CM_INTERFACE phbDetails;
    U8 flag = 0;
    U32 num;
    U8 number_ASCII[MAX_PB_NUMBER_LENGTH + 1 + 1];
    U8 *number_ptr;
    PHB_OPTIONAL_IDS_STRUCT *opt_ids = NULL;
    PHB_CALLER_GROUP_STRUCT *callerGroups;
    S8 *str_P = NULL;
    ecc_call_type_enum call_type;
    U8 *ecc_display_ptr;

#if defined(__MMI_INCOMING_CALL_VIDEO__)
    U16 video_id;
#endif 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    memset(number_ASCII, 0, ENCODING_LENGTH);
    memset((void*)&phbDetails, 0, sizeof(phbDetails));

    phbDetails.pictureId = IMG_PHB_DEFAULT;
    phbDetails.dialInList = MMI_PHB_NONE;
    mmi_ucs2_to_asc((PS8) number_ASCII, (PS8) number);

    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_CALL_GET_DATA_FOR_CALL_MGNT, g_phb_cntx.dial_from_list, is_mo_call);
    MMI_PRINT(MOD_MMI_COMMON_APP, MMI_PHB_TRACE_GROUP,"[PHB] mmi_phb_call_get_data_for_call_mgnt(),  number:%s", number);

    /* convert P to p */
    str_P = strstr((S8*)number_ASCII, "P");
    if (str_P)
    {
        str_P[0] = 'p'; 
    }

    number_ptr = number_ASCII;
    /* Ignore  *31# and #31# case */
    while (((number_ptr[0] == '*') && (number_ptr[1] == '3') && (number_ptr[2] == '1') && (number_ptr[3] == '#')) ||
           ((number_ptr[0] == '#') && (number_ptr[1] == '3') && (number_ptr[2] == '1') && (number_ptr[3] == '#')))
    {
        number_ptr += 4;
    }

    if (is_mo_call == TRUE)
    {
        /*  Display as Emergency Number */
    #ifdef __MMI_DUAL_SIM_MASTER__
        call_type = (ecc_call_type_enum) simInterface;
    #else
        call_type = ECC_SIM1_CALL;
    #endif
        if (ecc_custom_verify_emergency_number(
             number,
             (U8)mmi_ucs2strlen((S8*)number),
             ECC_ENCODING_UCS2,
             call_type,
             NULL,
             &ecc_display_ptr,
             NULL))
        {
            mmi_asc_to_ucs2((PS8) phbDetails.number, (S8*) number_ptr);
            g_phb_cntx.dial_from_list = MMI_PHB_NONE;   /* Clear dial from list flag */
            if (mmi_ucs2strlen((S8*) ecc_display_ptr) == 0)
            {
                mmi_ucs2ncpy((PS8) phbDetails.name, GetString(STR_EMERGENCY_NUMBER), MAX_PB_NAME_LENGTH);
            }
            else
            {
                mmi_ucs2ncpy((PS8) phbDetails.name, (PS8) ecc_display_ptr, MAX_PB_NAME_LENGTH);
            }
            if (GetUCS2Flag((PS8) phbDetails.name))
            {
                phbDetails.name_dcs = MMI_PHB_UCS2;
            }
            else
            {
                phbDetails.name_dcs = MMI_PHB_ASCII;
            }
            return phbDetails;
        }
    }
    /* Check if dial out from phonebook list */
    if (g_phb_cntx.dial_from_list == MMI_PHB_PHONEBOOK && is_mo_call)
    {
        g_phb_cntx.dial_from_list = MMI_PHB_NONE;
        flag = 1;
        store_index = g_phb_name_index[g_phb_cntx.active_index];
        phbDetails.dialInList = MMI_PHB_PHONEBOOK;
    }
    /* For example, from birthday */
    else if ((g_phb_cntx.dial_from_list == MMI_PHB_FROM_OTHER_APP) && is_mo_call
        && !g_phb_cntx.processing
    #ifdef __SYNCML_SUPPORT__
        && !(mmi_syncml_is_phb_sync_now())
    #endif /* __SYNCML_SUPPORT__ */
        )
    {
        g_phb_cntx.dial_from_list = MMI_PHB_NONE;
        flag = 1;
        store_index = g_phb_cntx.store_index;
        phbDetails.dialInList = MMI_PHB_FROM_OTHER_APP;
    }
    /*
     *  Check if dial out from FDN list,
     *  note that should enter here only from FDN list screen
     */
    else if (g_phb_cntx.dial_from_list == MMI_PHB_FDN && is_mo_call)
    {
        g_phb_cntx.dial_from_list = MMI_PHB_NONE;

        /* Fill up structure and return FDN data directly here */
        mmi_ucs2cpy(
            (S8*) phbDetails.name,
            (S8*) gFDLBDLContext.FDLBDLEntries[gFDLBDLContext.HighlightedFDN].alpha_id.name);
        phbDetails.name_dcs = gFDLBDLContext.FDLBDLEntries[gFDLBDLContext.HighlightedFDN].alpha_id.name_dcs;

        mmi_ucs2cpy(
            (S8*) phbDetails.number,
            (S8*) gFDLBDLContext.FDLBDLEntries[gFDLBDLContext.HighlightedFDN].tel.number);

        phbDetails.dialInList = MMI_PHB_FDN;
        return phbDetails;
    }
    /*
     *  Check if dial out from SDN list,
     *  note that should enter here only from SDN list screen
     */
    else if (g_phb_cntx.dial_from_list == MMI_PHB_SDN && is_mo_call)
    {
        MMI_PHB_ENTRY_STRUCT *sdn_entry;
    #ifdef __MMI_DUAL_SIM_MASTER__
        if (simInterface == SIM2)
        {
            sdn_entry = mmi_phb_get_slave_sim_entry();
        }
        else
    #endif /* __MMI_DUAL_SIM_MASTER__ */
        {
            sdn_entry = mmi_phb_get_sim_entry();
        }

        g_phb_cntx.dial_from_list = MMI_PHB_NONE;

        /* Fill up structure and return SDN data directly here */
        mmi_ucs2cpy((S8*) phbDetails.name, (S8*) sdn_entry->alpha_id.name);
        phbDetails.name_dcs = sdn_entry->alpha_id.name_dcs;
        mmi_ucs2cpy((S8*) phbDetails.number, (S8*) sdn_entry->tel.number);

        phbDetails.dialInList = MMI_PHB_SDN;
        return phbDetails;
    }
    else /* Not dial from list. */
    {
        /* Lookup into table when (1)  lookup table not empty (2) Not Processing (3) All Entries populated and sorted */
        if (g_phb_cntx.lookup_table_count && !g_phb_cntx.processing && (g_phb_cntx.populate_count == 0xffff)
        #ifdef __SYNCML_SUPPORT__
            && !(mmi_syncml_is_phb_sync_now())
        #endif /* __SYNCML_SUPPORT__ */
        )
        {
            num = mmi_phb_util_convert_number_to_int(number_ASCII);
            if (num < INVALID_NUMBER)
            {
                g_phb_cntx.searched_number_type = 0;
                store_index = mmi_phb_lookup_table_search(num, 0, (U16)(g_phb_cntx.lookup_table_count - 1), 
                    (S8*) number_ptr, MMI_STORAGE_BOTH);

                if (store_index < 0xffff)   /* i is storage location in array, begin from 0. */
                {
                    flag = 1;
                }
            }
        }
    }

    /* Map Number with one enrtry. */
    if (flag)
    {
        mmi_ucs2cpy((PS8) phbDetails.name, (PS8) PhoneBook[store_index].alpha_id.name);
        phbDetails.name_dcs = PhoneBook[store_index].alpha_id.name_dcs;
        if (phbDetails.dialInList != MMI_PHB_PHONEBOOK && phbDetails.dialInList != MMI_PHB_FROM_OTHER_APP) /* CM should use its number if dial from Phonebook list */
        {
        #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
            if ((store_index < MAX_PB_PHONE_ENTRIES) && g_phb_cntx.searched_number_type != 0)
            {
                switch (g_phb_cntx.searched_number_type)
                {
                    case MMI_PHB_ENTRY_FIELD_HOME:
                        mmi_asc_to_ucs2((PS8) phbDetails.number, (S8*) PhoneBookOptionalFields.homeNumber);
                        break;
                    case MMI_PHB_ENTRY_FIELD_OFFICE:
                        mmi_asc_to_ucs2((PS8) phbDetails.number, (S8*) PhoneBookOptionalFields.officeNumber);
                        break;
                    case MMI_PHB_ENTRY_FIELD_FAX:
                        mmi_asc_to_ucs2((PS8) phbDetails.number, (S8*) PhoneBookOptionalFields.faxNumber);
                        break;
                    default:
                        MMI_ASSERT(0);
                        break;
                }
            }
            else
        #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
            {
                mmi_phb_convert_get_ucs2_number((S8*) phbDetails.number, store_index);
            }
        }

        /* Caller Group Setup */
    #if !defined(__MMI_PHB_CALLERGROUP_IN_SIM__)    /* Associate caller group in SIM card entry */
        if (store_index < MAX_PB_PHONE_ENTRIES)
    #endif 
        {
            callerGroups = (PHB_CALLER_GROUP_STRUCT*) g_phb_cntx.caller_group;

            mmi_phb_read_optional_ids(store_index); /* i is store index */
            opt_ids = (PHB_OPTIONAL_IDS_STRUCT*) g_phb_cntx.optional_ids;

            grp_id = opt_ids->callerGroupID;
            if (grp_id)
            {
            #if defined(__MMI_CALLERGROUP_NO_ALERT__)
                phbDetails.alertType = MMI_ALERT_NONE;
            #else
                phbDetails.alertType = callerGroups[grp_id - 1].alertType;
            #endif
                phbDetails.backlightId = callerGroups[grp_id - 1].LEDPatternId;
                phbDetails.ringtoneId = callerGroups[grp_id - 1].ringToneID;
                if (callerGroups[grp_id - 1].pictureTagID == 1 /* Associate Picture is in the file system. */
                #if defined(__MMI_AVATAR__)
                    || callerGroups[grp_id - 1].pictureTagID == 2 /* Associate Picture is from avatar. */
                #endif
                    )
                {
                #if defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__)
                    phbDetails.pictureId = 1;
                    phbDetails.record_index = 5000 + grp_id;
                #endif /* defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__) */ 
                }
                else if (callerGroups[grp_id - 1].pictureTagID != 0)    /* 0 means default image */
                {
                    phbDetails.pictureId = callerGroups[grp_id - 1].pictureTagID;
                }

            #if defined(__MMI_INCOMING_CALL_VIDEO__)
                if (callerGroups[grp_id - 1].VideoID)
                {
                    phbDetails.videoId = callerGroups[grp_id - 1].VideoID;
                    phbDetails.video_record_index = (U16) (MAX_PB_PHONE_ENTRIES + grp_id);      /* MAX_PB_PHONE_ENTRIES for group 1 and so on. */
                }
            #endif /* defined(__MMI_INCOMING_CALL_VIDEO__) */ 
            }
        }

        /*  Personal Entry Setup, these will overwrite caller group setting. */
        if (store_index < MAX_PB_PHONE_ENTRIES)
        {
            /* Associate Pictures */
            if (opt_ids->pictureTagID == 1 
            #ifdef __MMI_AVATAR__
                || opt_ids->pictureTagID == 2
            #endif
                )
            {
            #if defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__)
                phbDetails.pictureId = 1;
                phbDetails.record_index = store_index + 1;  /* Record Must in NVRAM, so the record index is store_index + 1 */
            #endif /* defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__) */ 
            #if defined(__MMI_INCOMING_CALL_VIDEO__)
                phbDetails.videoId = 0;                     /* If Image Selected, should not play caller group's video */
            #endif 
            }
            else if ((opt_ids->pictureTagID != IMG_PHB_DEFAULT) && (opt_ids->pictureTagID != 0x0000))
            {
                phbDetails.pictureId = opt_ids->pictureTagID;
            #if defined(__MMI_INCOMING_CALL_VIDEO__)
                phbDetails.videoId = 0; /* If Image Selected, should not play caller group's video */
            #endif 
            }

            /* Associate Ring Tone */
            if (opt_ids->ringToneID)
            {
                phbDetails.ringtoneId = opt_ids->ringToneID;
            }

            /* For Incoming Call Video */
        #if defined(__MMI_INCOMING_CALL_VIDEO__)
            if ((video_id = mmi_phb_video_get_id_by_index(store_index)) != 0)
            {
                phbDetails.videoId = video_id;
                phbDetails.video_record_index = store_index + 1;        /* Record Must in NVRAM, so the record index is store_index + 1 */
            }
        #endif /* defined(__MMI_INCOMING_CALL_VIDEO__) */ 
        }
    }
    /*
     *   If entry is not found, try to search in FDN list to see if number can be match and dial out.
     *   This should be search when FDN is enable.
     *   Note: FDN list is stored in ADN storage when FDN is enable.
     */
#ifdef __MMI_DUAL_SIM_MASTER__
    else if (gSecuritySetupContext.FdlStatus && simInterface == SIM1)
#else /* __MMI_DUAL_SIM_MASTER__ */
    else if (gSecuritySetupContext.FdlStatus)
#endif /* __MMI_DUAL_SIM_MASTER__ */
    {
        mmi_ucs2_to_asc((PS8) number_ASCII, (PS8) number);
        store_index = mmi_phb_fdn_search_by_number((S8*) number_ASCII);
        if (store_index < 0xffff)
        {
            mmi_ucs2cpy((PS8) phbDetails.name, (PS8) PhoneBook[store_index].alpha_id.name);
            phbDetails.name_dcs = PhoneBook[store_index].alpha_id.name_dcs;

            mmi_phb_convert_get_ucs2_number((S8*) phbDetails.number, store_index);
        }
    }
#ifdef __MMI_DUAL_SIM_MASTER__
    else if (MTPNP_AD_FDN_GetStatus() && simInterface == SIM2)
    {
        mmi_ucs2_to_asc((PS8) number_ASCII, (PS8) number);
        store_index = MTPNP_PFAL_ADN_phb_fdn_search_by_number((S8*) number_ASCII);
        if (store_index < 0xffff)
        {
            mmi_ucs2cpy((PS8) phbDetails.name, (PS8) PhoneBook[store_index].alpha_id.name);
            phbDetails.name_dcs = PhoneBook[store_index].alpha_id.name_dcs;

            mmi_phb_convert_get_ucs2_number((S8*) phbDetails.number, store_index);
        }
    }
#endif /* __MMI_DUAL_SIM_MASTER__*/
    return phbDetails;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_call_set_dial_from_list
 * DESCRIPTION
 *  Let call management to set dial from list flag.
 * PARAMETERS
 *  value       [IN]        TRUE/FALSE value
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_call_set_dial_from_list(U8 value)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.dial_from_list = value;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_get_num_by_sim_index
 * DESCRIPTION
 *  Return the number to call mgnt for BT to call
 * PARAMETERS
 *  sim_location        [IN]        SIM1 or SIM2
 *  index               [IN]        physical location in SIM
 *  ucs2_num            [IN]        num buffer to write
 *  ucs2_len            [IN]        max usc2_strlen that can be written 
 *                             in ucs2_num buff (doesn't include null terminate)
 * RETURNS
 *  void
 *****************************************************************************/
MMI_BOOL mmi_phb_get_num_by_sim_index(U8 sim_location, U16 index, S8* ucs2_num, U16 ucs2_len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;
    U16 record_index;
    U8 hasEntry = 0;
    U16 phb_min_index, phb_max_index;    
    S8* num_buff;
    U16 num_max_len;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifndef __MMI_DUAL_SIM_MASTER__
    ASSERT(sim_location == SIM1);
#endif
    if (!mmi_phb_check_ready(MMI_TRUE))
    {
        return MMI_FALSE;
    }
#ifdef __MMI_DUAL_SIM_MASTER__
    else if (index == 0 || (sim_location == SIM1 && g_phb_cntx.sim_total == 0) || (sim_location == SIM2 && MTPNP_AD_ADN_SIM2GetUsedNumber() == 0))
#else
    else if (index == 0 || g_phb_cntx.sim_total == 0)
#endif
    {
        return MMI_FALSE;
    }
    else
    {
    #ifdef __MMI_DUAL_SIM_MASTER__
        if (sim_location == SIM2)
        {
            phb_min_index = MAX_PB_PHONE_ENTRIES + MAX_PB_SIM_ENTRIES;
            phb_max_index = MAX_PB_PHONE_ENTRIES + MAX_PB_SIM_ENTRIES + MAX_PB_SIM2_ENTRIES;
        }
        else
    #endif /* __MMI_DUAL_SIM_MASTER__ */
        {
            phb_min_index = MAX_PB_PHONE_ENTRIES;
            phb_max_index = MAX_PB_PHONE_ENTRIES + MAX_PB_SIM_ENTRIES;
        }

        for (i = 0; i < PhoneBookEntryCount; i++)
        {
            if (g_phb_name_index[i] >= phb_min_index && g_phb_name_index[i] < phb_max_index)
            {
                record_index = g_phb_name_index[i] - phb_min_index + 1;

                if (record_index == index)
                {
                    hasEntry = 1;
                    num_buff = OslMalloc((MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
                    memset(num_buff, 0, ((MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH));
                    mmi_phb_convert_get_ucs2_number(num_buff, g_phb_name_index[i]);
                    num_max_len = ((mmi_ucs2strlen(num_buff) >= ucs2_len) ? ucs2_len : mmi_ucs2strlen(num_buff));
                    mmi_ucs2ncpy(ucs2_num, num_buff, num_max_len);
                    memset((ucs2_num + num_max_len * ENCODING_LENGTH), 0, ENCODING_LENGTH);
                    //*(ucs2_num + num_max_len * ENCODING_LENGTH) = '\0';
                    //*(ucs2_num + num_max_len * ENCODING_LENGTH + 1) = NULL;
                    OslMfree(num_buff);
                    break;
                }
            }
        }

        if (hasEntry)
        {
            return MMI_TRUE;
        }
        else
        {
            return MMI_FALSE;
        }
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_set_global_store_index
 * DESCRIPTION
 *  Let other ap to set g_phb_cntx.store_index.
 * PARAMETERS
 *  value       [IN]        TRUE/FALSE value
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_set_global_store_index(U16 store_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.store_index = store_index;
}

#if defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__) || defined(__MMI_FILE_MANAGER__)


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_image_get_path_from_id
 * DESCRIPTION
 *  This function returns the phonebook associate picture image path by index.
 * PARAMETERS
 *  record_index        [IN]        Index of a phonebook record. (if >5000, it is a caller group image)
 * RETURNS
 *  full path of image
 *****************************************************************************/
S8 *mmi_phb_image_get_path_from_id(U16 record_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S16 drive;
    S8 *path;
    FS_HANDLE fh;
    S32 width, height;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_IMAGE_GET_PATH_FROM_ID_RECORD_INDEX, record_index);

    drive = MMI_PHB_DRV;

    if (drive > 0)
    {
        path = (S8*) phb_caller_pic_file_path;  /* Use global static buffer to store path */

        mmi_phb_image_get_full_path_by_index(record_index, path);

        /* Try to open file and check image format. If can't open, use default picture to display. */
        if (mmi_ucs2strlen(path) > 0)
        {
            fh = FS_Open((U16*) path, FS_READ_ONLY);

            MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_IMAGE_GET_PATH_FROM_ID_OPEN_FILE_RESULT, fh);
            if (fh)
            {
                FS_Close(fh);
                if (gdi_image_get_dimension_file((S8*) path, &width, &height) >= 0)
                {
                    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_IMAGE_GET_PATH_FROM_ID_GET_IMAGE_SUCCESS);
                    return path;
                }
            }
        }
    }

    return NULL;

}
#endif /* defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__) || defined(__MMI_FILE_MANAGER__) */ 

#if defined(__MMI_INCOMING_CALL_VIDEO__)


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_video_get_path_from_id
 * DESCRIPTION
 *  This function returns the phonebook associate video path by index.
 * PARAMETERS
 *  record_index        [IN]        Index of a phonebook record.
 * RETURNS
 *  full path of image
 *****************************************************************************/
S8 *mmi_phb_video_get_path_from_id(U16 record_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index = record_index - 1;
    U16 video_id = 0;

    PHB_CALLER_GROUP_STRUCT *callerGroups;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* In phonebook entry */
    if (store_index < MAX_PB_PHONE_ENTRIES)
    {
        video_id = mmi_phb_video_get_id_by_index(store_index);
    }
    /* In caller group */
    else if ((store_index < (MAX_PB_PHONE_ENTRIES + MAX_PB_CALLER_GROUPS)) && (store_index >= MAX_PB_PHONE_ENTRIES))
    {
        callerGroups = (PHB_CALLER_GROUP_STRUCT*) g_phb_cntx.caller_group;
        video_id = callerGroups[(store_index - MAX_PB_PHONE_ENTRIES)].VideoID;
    }
    return mmi_phb_caller_video_get_path_by_index(video_id);
}
#endif /* defined(__MMI_INCOMING_CALL_VIDEO__) */ 


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_call_enter_from_divert
 * DESCRIPTION
 *  Displays the PHB list for call divert
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_call_enter_from_divert(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_list_pre_entry_for_number_and_email(MMI_PHB_ENTRY_FIELD_ALL_NUMBER, SendPhbEntryForDivert);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_call_enter_from_active_call
 * DESCRIPTION
 *  Displays PHB main menu from active call options
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_call_enter_from_active_call(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mmi_phb_check_ready(MMI_TRUE))
    {
    	g_phb_enter_from = MMI_PHB_ENTER_NONE;
        mmi_phb_clear_old_history();
        mmi_phb_util_clear_buffer(TRUE);
        mmi_phb_entry_main_menu();
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_call_enter_from_deflect_option
 * DESCRIPTION
 *  Displays PHB list from incoming call deflect option
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_call_enter_from_deflect_option(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 entry_count;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mmi_phb_check_ready(MMI_TRUE))
    {
        phbListView = MMI_PHB_LIST_FOR_CM;
        g_phb_cntx.highlight_entry = 0;
        g_phb_cntx.list_filter = NULL;
        
        mmi_phb_nlf_entry_list_prepare();
        entry_count = mmi_phb_num_of_phb_contact_in_storage(g_phb_cntx.prefer_storage);

        mmi_phb_nlf_entry_list(
            entry_count,                        /* Total Entry */
            STR_SCR_PBOOK_VIEW_CAPTION,         /* Title String */
            IMG_SCR_PBOOK_CAPTION,              /* Title Image */
            STR_GLOBAL_OK,                      /* LSK */
            IMG_GLOBAL_OK,                      /* LSK */
            0,
            0,
            mmi_phb_get_index_nlf_second_level, /* Highlight Callback */
            mmi_phb_call_send_data_for_deflect, /* LSK Callback */
            NULL,
            mmi_phb_call_send_data_for_deflect, /* SEND Key Callback */
            mmi_phb_nlf_list_get_item,          /* List Callback */
            mmi_phb_nlf_list_get_hint,          /* Hint Callback */
            mmi_phb_call_enter_from_deflect_option,      /* Re-Entry Callback */
            TRUE,                               /* Alpha Index */
            TRUE);                              /* Right Arrow Key */
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_call_send_data_for_deflect
 * DESCRIPTION
 *  Sends the numbers info of the selected
 *  entry for Call deflect
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_call_send_data_for_deflect(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S8* phb_deflect_num;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phb_deflect_num = OslMalloc((MAX_PB_NUMBER_LENGTH+1+1)*ENCODING_LENGTH);
    mmi_phb_convert_get_ucs2_number(phb_deflect_num, g_phb_name_index[g_phb_cntx.active_index_second]);        /* BCD number format. */
    CBackCallDeflectNumberFromPB(phb_deflect_num);
    DeleteScreenIfPresent(SCR_PBOOK_LIST);
    OslMfree(phb_deflect_num);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_list_enter_first_from_active_call
 * DESCRIPTION
 *  Enter phonebook list from active call to first item.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_list_enter_first_from_active_call(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_list_enter_from_active_call(0);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_list_enter_last_from_active_call
 * DESCRIPTION
 *  Enter phonebook list from active call to last item.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_list_enter_last_from_active_call(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 last_entry = mmi_phb_num_of_phb_contact_in_storage(g_phb_cntx.prefer_storage);

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_list_enter_from_active_call((U16) (last_entry - 1));
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_list_enter_from_active_call
 * DESCRIPTION
 *  enter phonebook list from active call, highlight on specific entry.
 *  (Note this function call will clear all phonebook history if exists)
 * PARAMETERS
 *  highlight_pos       [IN]        Highlight entry position.
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_list_enter_from_active_call(U16 highlight_pos)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mmi_phb_check_ready(MMI_TRUE))
    {
        if (mmi_phb_num_of_phb_contact_in_storage(g_phb_cntx.prefer_storage))
        {
            phbListView = MMI_PHB_LIST_FOR_PHB;
            g_phb_cntx.highlight_entry = highlight_pos;

            mmi_phb_clear_old_history();    /* Clear old phb history */
            mmi_phb_list_pre_entry();
        }
        else
        {
            mmi_display_popup((UI_string_type)GetString(STR_NO_ENTRIES_MESSAGE), MMI_EVENT_FAILURE);
        }
    }
}


#define MMI_PHB_INTERFACE_FOR_SMS
#if defined(__MMI_PHB_GENERIC_MULTI_SELECT__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_generic_multi_select_list_pre_entry
 * DESCRIPTION
 *  Pre-entry function of multi select list used for other AP (SMS, MMS...)
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_generic_multi_select_list_pre_entry(U16 filter_type, U16 select_limit, mmi_phb_get_index_from_group_callback_type list_view_call_back, mmi_phb_check_selected_index_callback_type list_check_call_back)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(list_view_call_back);
    phb_list_view_type = filter_type;
    phb_mark_several_list_call_back = NULL;
    phb_mark_several_check_call_back = NULL;
    phb_generic_multi_select_list_call_back = list_view_call_back;
    phb_generic_multi_select_check_call_back = list_check_call_back;
    g_phb_multi_select_limit = select_limit;
#ifdef __MMI_PHB_QUICK_SEARCH__
    if (IsScreenPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT_AND_QUICK_SEARCH))
    {
        DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT_AND_QUICK_SEARCH);
    }
    mmi_phb_entry_generic_multi_select_and_quick_search_list();
#else /* __MMI_PHB_QUICK_SEARCH__ */
    DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT);
    mmi_phb_entry_generic_multi_select_list();
#endif /* __MMI_PHB_QUICK_SEARCH__ */
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mark_several_for_msg_pre_entry
 * DESCRIPTION
 *  Pre-entry function of mark several list used for message (SMS, MMS...)
 * PARAMETERS
 *  filter_type     [IN]        
 *  select_limit    [IN]        
 *  list_view_cb    [IN]        
 *  group_view_cb   [IN]        
 *  list_check_cb   [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_mark_several_for_msg_pre_entry(
        U16 filter_type,
        U16 select_limit,
        mmi_phb_get_number_type_callback_type list_view_cb,
        mmi_phb_get_number_type_callback_type group_view_cb,
        mmi_phb_check_number_type_callback_type list_check_cb)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(list_view_cb);
#if defined(__MMI_MESSAGES_SEND_BY_GROUP__) || defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__) || defined(__MMI_JATAAYU_MMS_ADD_NUMBER_BY_GROUP__)
    MMI_ASSERT(group_view_cb);
#endif
#ifdef __MMI_OP12_PHB__
    phb_list_view_type = MMI_PHB_ENTRY_FIELD_NUMBER;
#else
    phb_list_view_type = filter_type;
#endif
    phb_mark_several_list_call_back = list_view_cb;
    phb_mark_several_check_call_back = list_check_cb;
#if defined(__MMI_MESSAGES_SEND_BY_GROUP__) || defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__) || defined(__MMI_JATAAYU_MMS_ADD_NUMBER_BY_GROUP__)
    phb_mark_several_group_call_back = group_view_cb;
#endif /* defined(__MMI_MESSAGES_SEND_BY_GROUP__) || defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__) || defined(__MMI_JATAAYU_MMS_ADD_NUMBER_BY_GROUP__) */
    phb_generic_multi_select_list_call_back = NULL;
    phb_generic_multi_select_check_call_back = NULL;
    g_phb_multi_select_limit = select_limit;
#ifdef __MMI_PHB_QUICK_SEARCH__
    if (IsScreenPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT_AND_QUICK_SEARCH))
    {
        DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT_AND_QUICK_SEARCH);
    }
    mmi_phb_entry_generic_mark_several_and_quick_search_list();
#else /* __MMI_PHB_QUICK_SEARCH__ */
    DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT);
    mmi_phb_entry_generic_mark_several_list();
#endif /* __MMI_PHB_QUICK_SEARCH__ */
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mark_several_for_msg_get_number
 * DESCRIPTION
 *  Get entry function of mark several list used for message (SMS, MMS...)
 * PARAMETERS
 *  dest            [IN]        Dest buffer
 *  max_dest_len    [IN]        Size of dest buffer in bytes
 *  PhbIndex        [IN]        Phb store index
 *  num_type        [IN]        Number type
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_mark_several_for_msg_get_number(U8 *dest, U8 max_dest_len, U16 PhbIndex, U8 num_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch (num_type)
    {
        case MMI_PHB_NUMBER_TYPE_NUMBER:
            {
                U8 tempNumber[MAX_PB_NUMBER_LENGTH + 1 + 1];
                U8 tempNumber2[MAX_PB_NUMBER_LENGTH + 1 + 1];

                if (((PhoneBook[PhbIndex].tel.type & 0x90) == 0x90) && (PhoneBook[PhbIndex].tel.type != 0xFF))
                {
                    mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber2, PhoneBook[PhbIndex].tel.number, max_dest_len, PhbIndex);
                    /* now number is +**21*8123456# */
                    /* call the third API to transfer + to place just after SS pattern */
                    applib_decode_number(tempNumber2, tempNumber, 0x91);
                    /* now number is **21*+8123456#, so we can show the original data to user */
                }
                else
                {
                    mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, PhoneBook[PhbIndex].tel.number, max_dest_len, PhbIndex);
                }

                mmi_asc_to_ucs2((S8*) dest, (S8*) tempNumber);
            }
            break;
        case MMI_PHB_NUMBER_TYPE_EMAIL_ADDRESS:
            {
            #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)    
                S16 pError;
            #endif
                PHB_OPTIONAL_IDS_STRUCT *opt_ids;

                mmi_phb_read_optional_ids(PhbIndex);
                opt_ids = (PHB_OPTIONAL_IDS_STRUCT*) g_phb_cntx.optional_ids;

            #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
                ReadRecord(
                    NVRAM_EF_PHB_FIELDS_LID,
                    (U16) (PhbIndex + 1),
                    (void*)&PhoneBookOptionalFields,
                    OPTIONAL_FIELDS_RECORD_SIZE,
                    &pError);
                mmi_asc_n_to_ucs2((S8*) dest, (PS8) PhoneBookOptionalFields.emailAddress, max_dest_len);
            #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
            }
            break;
        case MMI_PHB_NUMBER_TYPE_EMAIL_ADDRESS_2:
            {
            #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)    
                S16 pError;
            #endif
                PHB_OPTIONAL_IDS_STRUCT *opt_ids;

                mmi_phb_read_optional_ids(PhbIndex);
                opt_ids = (PHB_OPTIONAL_IDS_STRUCT*) g_phb_cntx.optional_ids;

            #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
                ReadRecord(
                    NVRAM_EF_PHB_FIELDS_LID,
                    (U16) (PhbIndex + 1),
                    (void*)&PhoneBookOptionalFields,
                    OPTIONAL_FIELDS_RECORD_SIZE,
                    &pError);
            #if defined(__MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__)
                mmi_asc_n_to_ucs2((S8*) dest, (PS8) PhoneBookOptionalFields.emailAddress2, max_dest_len);
            #else
                MMI_ASSERT(0);
            #endif
            #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
            }
            break;
    }
}
#endif /* defined(__MMI_PHB_GENERIC_MULTI_SELECT__) */


#if defined(__MMI_PHB_QUICK_SEARCH__) && defined(__MMI_PHB_GENERIC_MULTI_SELECT__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_generic_multi_select_and_quick_search_list_pre_entry
 * DESCRIPTION
 *  Pre-entry function of multi select list used for other AP (SMS, MMS...)
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_generic_multi_select_and_quick_search_list_pre_entry(U16 filter_type, U16 select_limit, mmi_phb_get_index_from_group_callback_type list_view_call_back, mmi_phb_check_selected_index_callback_type list_check_call_back)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    //MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_GENERIC_MULTI_SELECT_AND_QUICK_SEARCH);
    phb_list_view_type = filter_type;
    phb_generic_multi_select_list_call_back = list_view_call_back;
    phb_generic_multi_select_check_call_back = list_check_call_back;
    g_phb_multi_select_limit = select_limit;
    if (IsScreenPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT_AND_QUICK_SEARCH))
    {
        DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT_AND_QUICK_SEARCH);
    }
    mmi_phb_entry_generic_multi_select_and_quick_search_list();    	
}
#endif /* defined(__MMI_PHB_QUICK_SEARCH__) && defined(__MMI_PHB_GENERIC_MULTI_SELECT__) */

#if defined(__MMI_MESSAGES_SEND_BY_GROUP__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_sms_enter_send_by_group
 * DESCRIPTION
 *  Displays the Caller Groups list for SMS to send message
 *  to all numbers belonging to this group
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_sms_enter_send_by_group(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_get_address_from_caller_group((U8)MMI_PHB_ENTRY_FIELD_NUMBER, mmi_msg_send_msg_to_group);
}
#endif /* defined(__MMI_MESSAGES_SEND_BY_GROUP__) */ 


#if defined(__MMI_MESSAGES_SEND_BY_GROUP__) || defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__) || defined(__MMI_JATAAYU_MMS_ADD_NUMBER_BY_GROUP__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_sms_send_data_for_group
 * DESCRIPTION
 *  Sends the names & numbers info of the selected
 *  caller group to SMS
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_sms_send_data_for_group(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 *indices;
    U16 i, j = 0;
    S16 pError;
    U16 store_index;
    U8 optid_record, optid_index, current_record = 0;
    PHB_OPTIONAL_IDS_STRUCT PhbOptIDs[OPTIONAL_IDS_RECORD_TOTAL];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* Clear key immediately to avoid user press twice. */
    ClearKeyHandler(KEY_LSK, KEY_EVENT_UP);

#ifdef __MMI_PHB_USIM_FIELD__
    if (g_phb_cntx.active_index >= MAX_PB_CALLER_GROUPS)
    {
    #if (MAX_PB_ENTRIES >= 1000)
        indices = (U16*) g_phb_list_filter;
    #else
        indices = OslMalloc(MAX_PB_SIM_ENTRIES * sizeof(U16));
    #endif /* MAX_PB_ENTRIES >= 1000 */
        for (i = 0; i < PhoneBookEntryCount; ++i)
        {
            store_index = g_phb_name_index[i];
            if (store_index >= MAX_PB_PHONE_ENTRIES)
            {
                /* Compare caller group ID */
                if (g_phb_cntx.group_type[store_index - MAX_PB_PHONE_ENTRIES] == 
                    (g_phb_cntx.active_index_second + 1 - MAX_PB_CALLER_GROUPS) && 
                    PhoneBook[store_index].field & phb_list_view_type)
                {
                    indices[j++] = store_index;
                }
            }
        }
        MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_SMS_SEND_DATA_FOR_GROUP_MEMBER_COUNT, j);
        phb_group_view_call_back(j, indices);
    #if (MAX_PB_ENTRIES < 1000)
        OslMfree(indices);
    #endif /* MAX_PB_ENTRIES < 1000 */
        return;
    }
#endif /* __MMI_PHB_USIM_FIELD__ */

#if (MAX_PB_ENTRIES >= 1000)
    indices = (U16*) g_phb_list_filter;
#else /* (MAX_PB_ENTRIES >= 1000) */ 
#if defined(__MMI_PHB_CALLERGROUP_IN_SIM__)     /* Associate caller group in SIM card entry */
    indices = OslMalloc(MAX_PB_ENTRIES * sizeof(U16));
#else 
    indices = OslMalloc(MAX_PB_PHONE_ENTRIES * sizeof(U16));
#endif 
#endif /* MAX_PB_ENTRIES >= 1000 */

    for (i = 0; i < PhoneBookEntryCount; ++i)
    {
        store_index = g_phb_name_index[i];

    #if !defined(__MMI_PHB_CALLERGROUP_IN_SIM__)    /* Associate caller group in SIM card entry */
        if (store_index < MAX_PB_PHONE_ENTRIES)
    #endif 
        {
            /* Get optional IDs record. */
            optid_record = (store_index / OPTIONAL_IDS_RECORD_TOTAL) + 1;
            optid_index = store_index - (optid_record - 1) * OPTIONAL_IDS_RECORD_TOTAL;

            if (optid_record != current_record)
            {
                ReadRecord(NVRAM_EF_PHB_IDS_LID, optid_record, (void*)PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);
                current_record = optid_record;
            }
            /* Compare caller group ID */
            if (PhbOptIDs[optid_index].callerGroupID == (g_phb_cntx.active_index_second + 1) &&
                PhoneBook[store_index].field & phb_list_view_type)
            {
                indices[j++] = store_index;
            }
        }
    }

    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_SMS_SEND_DATA_FOR_GROUP_MEMBER_COUNT, j);

    phb_group_view_call_back(j, indices);
#if (MAX_PB_ENTRIES < 1000)
    OslMfree(indices);
#endif /* MAX_PB_ENTRIES < 1000 */
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_get_caller_group_data
 * DESCRIPTION
 *  Get the all numbers belonging to this group for UM to send message
 * PARAMETERS
 *  address_type                 [IN]        address type: number or e-mail
 *  get_address_call_back        [IN]        call back function after user chooses a group
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_get_address_from_caller_group(U8 address_type, mmi_phb_get_index_from_group_callback_type get_address_call_back)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __SYNCML_SUPPORT__
    if (mmi_syncml_is_phb_sync_now())
    {
        mmi_phb_entry_not_ready(STR_ID_SYNC_PLEASE_WAIT);
    }
    else
#endif /* __SYNCML_SUPPORT__ */
#ifdef __MMI_DUAL_SIM_MASTER__
    if (MTPNP_PFAL_Phb_IsReady() == MTPNP_TRUE)
#else /* __MMI_DUAL_SIM_MASTER__ */
    if (g_phb_cntx.phb_ready && !g_phb_cntx.processing)
#endif /* __MMI_DUAL_SIM_MASTER__ */
    {
        if (PhoneBookEntryCount)
        {
            phbListView = MMI_PHB_LIST_FOR_GET_ADDRESS_FROM_GROUP;
            phb_list_view_type = address_type;
            phb_group_view_call_back = get_address_call_back;
            mmi_phb_entry_callergroup();    /* Entry caller group main screen. */
        }
        else
        {
            mmi_display_popup((UI_string_type)GetString(STR_NO_ENTRIES_MESSAGE), MMI_EVENT_FAILURE);
        }
    }
    else
    {
        mmi_phb_entry_not_ready(STR_PROCESSING_PHONEBOOK);
    }
}
#endif /* defined(__MMI_MESSAGES_SEND_BY_GROUP__) || defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__) || defined(__MMI_JATAAYU_MMS_ADD_NUMBER_BY_GROUP__) */ 


#if defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__) || defined(__MMI_JATAAYU_MMS_ADD_NUMBER_BY_GROUP__) 
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_get_email_p
 * DESCRIPTION
 *  Get the all numbers belonging to this group for UM to send message
 * PARAMETERS
 *  store_index                 [IN]        store index of the phonebook entry
 *  address_length              [OUT]       email address length
 * RETURNS
 *  email address pointer
 *****************************************************************************/
U8* mmi_phb_get_email_p(U16 store_index, U8* address_length)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S16 pError;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
    if (store_index < MAX_PB_PHONE_ENTRIES) /* in NVRAM */
    {
        ReadRecord(
            NVRAM_EF_PHB_FIELDS_LID,
            (U16) (store_index + 1),
            (void*)&PhoneBookOptionalFields,
            OPTIONAL_FIELDS_RECORD_SIZE,
            &pError);
        *address_length = strlen((PS8)PhoneBookOptionalFields.emailAddress);
        return (U8*)PhoneBookOptionalFields.emailAddress;
    }
#endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
#ifdef __MMI_PHB_USIM_FIELD__
    if (store_index >= MAX_PB_PHONE_ENTRIES)    /* in USIM */
    {
        *address_length = phb_email[store_index - MAX_PB_PHONE_ENTRIES].email_length;
        return (U8*) phb_email[store_index - MAX_PB_PHONE_ENTRIES].email_address;
    }
#endif /* __MMI_PHB_USIM_FIELD__ */
    *address_length = 0;
    return NULL;
}
#endif /* defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__) || defined(__MMI_JATAAYU_MMS_ADD_NUMBER_BY_GROUP__) */


#ifdef __MMI_PHB_SEND_MSG__
#ifdef __MMI_UNIFIED_COMPOSER__
static void mmi_phb_uc_send_msg_choose_done(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_uc_send_msg((PS8) g_phb_cntx.number_to_dial[g_phb_cntx.active_index_third]);
    
    DeleteScreenIfPresent(SCR_CHOOSE_NUMBER);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_uc_send_msg_choose_entry
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_uc_send_msg_choose_entry(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_entry_choose_field(
        g_phb_name_index[g_phb_cntx.active_index],
        (MMI_PHB_ENTRY_FIELD_GSM_NUM | MMI_PHB_ENTRY_FIELD_EMAIL),
        mmi_phb_uc_send_msg_choose_entry,
        mmi_phb_uc_send_msg_choose_done,
        GoBackHistory,
        NULL,
        MMI_FALSE);
}

#endif /* __MMI_UNIFIED_COMPOSER__ */
#endif /* __MMI_PHB_SEND_MSG__ */


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_sms_send_data_choose_entry
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_sms_send_data_choose_entry(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phbListView = MMI_PHB_LIST_FOR_SMS;

    mmi_phb_entry_choose_field(
        g_phb_name_index[g_phb_cntx.active_index],
        MMI_PHB_ENTRY_FIELD_GSM_NUM,
        mmi_phb_sms_send_data_choose_entry,
        mmi_phb_sms_send_data_for_entry_list,
        GoBackHistory,
        NULL,
        MMI_FALSE);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_sms_send_data_for_entry_name_only
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_sms_send_data_for_entry_name_only(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_sms_send_data_for_entry(NULL);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_sms_send_data_for_entry_list
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_sms_send_data_for_entry_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* DeleteNScrId(SCR_PBOOK_LIST); */
    mmi_phb_sms_send_data_for_entry((PS8) g_phb_cntx.number_to_dial[g_phb_cntx.active_index_third]);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_sms_send_data_for_entry
 * DESCRIPTION
 *  Sends the name & number info of the selected PHB entry to SMS
 * PARAMETERS
 *  number      [IN]        Input number string
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_sms_send_data_for_entry(S8 *number)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index;
    S8 ascii_number[MAX_PB_NUMBER_LENGTH + 1 + 1];
    mmi_frm_sms_api_entry_write_struct sendData;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    store_index = g_phb_name_index[g_phb_cntx.active_index_second];

    if (mmi_ucs2strlen((PS8) number) == 0)
    {
        mmi_display_popup((UI_string_type)GetString(STR_SMS_NUM_CANNOT_BE_EMPTY), MMI_EVENT_FAILURE);
    }
    else
    {
        memset(ascii_number, 0, sizeof(ascii_number));
        memset(&sendData, 0, sizeof(mmi_frm_sms_api_entry_write_struct));

        mmi_ucs2_n_to_asc((PS8)ascii_number, (PS8)number, MAX_PB_NUMBER_LENGTH + 1);
        sendData.string = NULL;
        sendData.stringlength = 0;
        sendData.dcs = 0;
        sendData.flag = MMI_FRM_SMS_ENTRY_WRITE_REPLY;
        sendData.ascii_addr = (U8*) ascii_number;

        mmi_frm_sms_api_entry_write_msg(&sendData);

        DeleteScreenIfPresent(SCR_CHOOSE_NUMBER);
    }
}


#ifdef __MMI_MMS__
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mms_send_data_for_entry
 * DESCRIPTION
 *  Sends the name & number info of the selected PHB entry to SMS
 * PARAMETERS
 *  number      [IN]        Input number string
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_mms_send_data_for_entry(S8 *number)
{
#ifdef MMS_SUPPORT
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mmi_ucs2strlen((PS8) number) <= MAX_PB_EMAIL_LENGTH)
    {
        if (mmi_ucs2strlen((PS8) number) == 0)
        {
            mmi_display_popup((UI_string_type)GetString(STR_SMS_NUM_CANNOT_BE_EMPTY), MMI_EVENT_FAILURE);
        }
        else
        {
            mms_address_insert_hdlr((char*)number);
            DeleteScreenIfPresent(SCR_CHOOSE_NUMBER);
        }
    }
    else
#endif /* MMS_SUPPORT */
    {
        mmi_display_popup((UI_string_type)GetString(STR_SMS_NUM_LEN_EXCEEDED), MMI_EVENT_FAILURE);
    }
    //phbListView = MMI_PHB_LIST_FOR_NONE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mms_send_data_for_entry_list
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_mms_send_data_for_entry_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_mms_send_data_for_entry((PS8) g_phb_cntx.number_to_dial[g_phb_cntx.active_index_third]);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mms_send_data_choose_entry
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_mms_send_data_choose_entry(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phbListView = MMI_PHB_LIST_FOR_MMS_ALL;

    mmi_phb_entry_choose_field(
        g_phb_name_index[g_phb_cntx.active_index],
        MMI_PHB_ENTRY_FIELD_MMS,
        mmi_phb_mms_send_data_choose_entry,
        mmi_phb_mms_send_data_for_entry_list,
        GoBackHistory,
        NULL,
        MMI_FALSE);
}
#endif /* __MMI_MMS__ */ 


#define MMI_PHB_UPDATE_INLINE_LIST
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_update_delete_image
 * DESCRIPTION
 *  Deletes the image id from PHB records if
 *  one of the downloaded/factory/system images
 *  are deleted.
 * PARAMETERS
 *  imageId     [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_update_delete_image(U16 imageId)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;
    U8 flag = 0;
    S16 pError;
    U8 optid_record = 0, optid_index, current_record = 0;
    PHB_OPTIONAL_IDS_STRUCT PhbOptIDs[OPTIONAL_IDS_RECORD_TOTAL];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (i = 0; i < MAX_PB_PHONE_ENTRIES; ++i)
    {
        /* Get optional IDs record. */
        optid_record = (i / OPTIONAL_IDS_RECORD_TOTAL) + 1;
        optid_index = i - (optid_record - 1) * OPTIONAL_IDS_RECORD_TOTAL;

        if (optid_record != current_record)
        {
            if (current_record != 0 && flag == 1)   /* Write result back for previous optional ID record. */
            {
                WriteRecord(NVRAM_EF_PHB_IDS_LID, current_record, (void*)PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);
                flag = 0;
            }

            /* Read out next record. */
            ReadRecord(NVRAM_EF_PHB_IDS_LID, optid_record, (void*)PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);
            current_record = optid_record;
        }

        if (PhbOptIDs[optid_index].pictureTagID == imageId)
        {
            PhbOptIDs[optid_index].pictureTagID = IMG_PHB_DEFAULT;
            flag = 1;
        }
    }

    /* Write back for last record. */
    if (flag)
    {
        WriteRecord(NVRAM_EF_PHB_IDS_LID, optid_record, (void*)PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_update_delete_image_caller_group
 * DESCRIPTION
 *  Deletes the image id from caller group if
 *  one of the downloaded/factory/system images
 *  are deleted.
 * PARAMETERS
 *  imageId     [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_update_delete_image_caller_group(U16 imageId)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 i;
    U8 flag = 0;
    S16 pError;
    PHB_CALLER_GROUP_STRUCT *callerGroups;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    callerGroups = (PHB_CALLER_GROUP_STRUCT*) g_phb_cntx.caller_group;

    for (i = 0; i < MAX_PB_CALLER_GROUPS; ++i)
    {
        if (callerGroups[i].pictureTagID == imageId)
        {
            callerGroups[i].pictureTagID = IMG_PHB_DEFAULT;
            flag = 1;
        }
    }

    if (flag)
    {
        WriteRecord(NVRAM_EF_PHB_CALLER_GROUPS_LID, 1, callerGroups, CALLER_GROUPS_RECORD_SIZE, &pError);
    }

}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_update_delete_audio
 * DESCRIPTION
 *  Deletes the audio id from PHB records if
 *  one of the downloaded/factory/system/composed
 *  images are deleted.
 * PARAMETERS
 *  audioId     [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_update_delete_audio(U16 audioId)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;
    U8 flag = 0;
    S16 pError;
    U8 optid_record = 0, optid_index, current_record = 0;
    PHB_OPTIONAL_IDS_STRUCT PhbOptIDs[OPTIONAL_IDS_RECORD_TOTAL];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (i = 0; i < MAX_PB_PHONE_ENTRIES; ++i)
    {
        /* Get optional IDs record. */
        optid_record = (i / OPTIONAL_IDS_RECORD_TOTAL) + 1;
        optid_index = i - (optid_record - 1) * OPTIONAL_IDS_RECORD_TOTAL;

        if (optid_record != current_record)
        {
            if (current_record != 0 && flag == 1)   /* Write result back for previous optional ID record. */
            {
                WriteRecord(NVRAM_EF_PHB_IDS_LID, current_record, (void*)PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);
                flag = 0;
            }

            /* Read out next record. */
            ReadRecord(NVRAM_EF_PHB_IDS_LID, optid_record, (void*)PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);
            current_record = optid_record;
        }

        if (PhbOptIDs[optid_index].ringToneID == audioId)
        {
            PhbOptIDs[optid_index].ringToneID = 0;
            flag = 1;
        }
    }

    /* Write back for last record. */
    if (flag)
    {
        WriteRecord(NVRAM_EF_PHB_IDS_LID, optid_record, (void*)PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_update_delete_audio_caller_group
 * DESCRIPTION
 *  Deletes the audio id from CallerGroup records if
 *  one of the downloaded/factory/system/composed
 *  images are deleted.
 * PARAMETERS
 *  audioId     [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_update_delete_audio_caller_group(U16 audioId)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 i, flag = 0;
    S16 pError;
    PHB_CALLER_GROUP_STRUCT *callerGroups;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    callerGroups = (PHB_CALLER_GROUP_STRUCT*) g_phb_cntx.caller_group;

    for (i = 0; i < MAX_PB_CALLER_GROUPS; ++i)
    {
        if (callerGroups[i].ringToneID == audioId)
        {
            callerGroups[i].ringToneID = 0;
            flag = 1;
        }
    }

    if (flag)
    {
        WriteRecord(NVRAM_EF_PHB_CALLER_GROUPS_LID, 1, callerGroups, CALLER_GROUPS_RECORD_SIZE, &pError);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_pre_send_data
 * DESCRIPTION
 *  Send choosed number to MMS
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_pre_send_data(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phb_list_view_call_back(pbName, (S8*) g_phb_cntx.number_to_dial[g_phb_cntx.active_index_third]);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_choose_number
 * DESCRIPTION
 *  Entry Choose entry screen if there are more then one entry with number field.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_choose_number(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;
    U8 *guiBuffer;
    U16 numberTypeImageList[5];
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)    
    S16 pError;
#endif
    U16 store_index;
    U8  title_string[(MAX_PB_NAME_LENGTH + 1) *ENCODING_LENGTH];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (phbListView == MMI_PHB_LIST_FOR_MMS_ALL)
    {
        store_index = g_phb_name_index[g_phb_cntx.active_index_second];
    }
    else
    {
        store_index = g_phb_cntx.active_index_second;
    }

    mmi_ucs2cpy(pbName, (PS8) PhoneBook[store_index].alpha_id.name);
    /* For SMS insert phonebook name */
    if (phb_list_view_type == MMI_PHB_ENTRY_FIELD_NAME)
    {
        g_phb_cntx.number_to_dial[0] = NULL;
        g_phb_cntx.active_index_third = 0;
    #if defined(MBM_IMIBASE_SUPPORT)
        if(mbm_sys_is_in_imiworld())
        {
            mbm_phb_pre_send_data();
        }
        else
        {
            mmi_phb_pre_send_data();
        }
    #else//MBM_IMIBASE_SUPPORT
        mmi_phb_pre_send_data();
    #endif//MBM_IMIBASE_SUPPORT
        return;
    }
    mmi_phb_convert_get_ucs2_number(pbNumber, store_index); /* BCD number format. */

#ifndef __MMI_PHB_USIM_FIELD__
    if ((phb_list_view_type & MMI_PHB_ENTRY_FIELD_ALL_NUMBER) && (store_index >= MAX_PB_PHONE_ENTRIES))   /* In SIM */
    {
        if (!mmi_ucs2strlen(pbNumber))
        {
            mmi_display_popup((UI_string_type)GetString(STR_NO_NUMBER_TO_DIAL), MMI_EVENT_FAILURE);
        }
        else
        {
            g_phb_cntx.number_type[0] = MMI_PHB_ENTRY_FIELD_NUMBER;
            g_phb_cntx.number_to_dial[0] = (U8*) pbNumber;
            g_phb_cntx.active_index_third = 0;
        #if defined(MBM_IMIBASE_SUPPORT)
            if(mbm_sys_is_in_imiworld())
            {
                mbm_phb_pre_send_data();
            }
            else
            {
                mmi_phb_pre_send_data();
            }
        #else//MBM_IMIBASE_SUPPORT
            mmi_phb_pre_send_data();
        #endif//MBM_IMIBASE_SUPPORT
        }
    }
    else
#endif /* __MMI_PHB_USIM_FIELD__ */ 
    {
        i = 0;

        if ((phb_list_view_type& MMI_PHB_ENTRY_FIELD_ALL_NUMBER) && mmi_ucs2strlen(pbNumber))
        {
            g_phb_cntx.number_type[i] = MMI_PHB_ENTRY_FIELD_NUMBER;
            g_phb_cntx.number_to_dial[i] = (U8*) pbNumber;
            numberTypeImageList[i++] = IMG_MOBILE_NUMBER;
        }
    #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
    #ifdef __MMI_PHB_USIM_FIELD__
        if (store_index >= MAX_PB_PHONE_ENTRIES)
        {
            mmi_phb_op_set_option_data_by_usim_index((U16) store_index, &PhoneBookOptionalFields);
        }
        else
    #endif /* __MMI_PHB_USIM_FIELD__ */ 
        {
            ReadRecord(
                NVRAM_EF_PHB_FIELDS_LID,
                (U16) (store_index + 1),
                (void*)&PhoneBookOptionalFields,
                OPTIONAL_FIELDS_RECORD_SIZE,
                &pError);
        }
        if (phb_list_view_type & MMI_PHB_ENTRY_FIELD_ALL_NUMBER)
        {
            mmi_asc_to_ucs2(pbHomeNumber, (PS8) PhoneBookOptionalFields.homeNumber);
            if (mmi_ucs2strlen(pbHomeNumber))
            {
                g_phb_cntx.number_type[i] = MMI_PHB_ENTRY_FIELD_HOME;
                g_phb_cntx.number_to_dial[i] = (PU8) pbHomeNumber;
                numberTypeImageList[i++] = IMG_HOME_NUMBER;
            }
            mmi_asc_to_ucs2(pbOfficeNumber, (PS8) PhoneBookOptionalFields.officeNumber);
            if (mmi_ucs2strlen(pbOfficeNumber))
            {
                g_phb_cntx.number_type[i] = MMI_PHB_ENTRY_FIELD_OFFICE;
                g_phb_cntx.number_to_dial[i] = (PU8) pbOfficeNumber;
                numberTypeImageList[i++] = IMG_OFFICE_NUMBER;
            }
            mmi_asc_to_ucs2(pbFaxNumber, (PS8) PhoneBookOptionalFields.faxNumber);
            if (mmi_ucs2strlen(pbFaxNumber))
            {
                g_phb_cntx.number_type[i] = MMI_PHB_ENTRY_FIELD_FAX;
                g_phb_cntx.number_to_dial[i] = (PU8) pbFaxNumber;
                numberTypeImageList[i++] = IMG_FAX_NUMBER;
            }
        }

        if (phb_list_view_type & MMI_PHB_ENTRY_FIELD_EMAIL)
        {
            mmi_asc_to_ucs2(pbEmailAddress, (PS8) PhoneBookOptionalFields.emailAddress);
            if (mmi_ucs2strlen(pbEmailAddress))
            {
                g_phb_cntx.number_to_dial[i] = (PU8) pbEmailAddress;
                numberTypeImageList[i++] = IMG_EMAIL_ADDRESS;
            }
        #if defined(__MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__)
            mmi_asc_to_ucs2(pbEmailAddress2, (PS8) PhoneBookOptionalFields.emailAddress2);
            if (mmi_ucs2strlen(pbEmailAddress2))
            {
                g_phb_cntx.number_to_dial[i] = (PU8) pbEmailAddress2;
                numberTypeImageList[i++] = IMG_EMAIL_ADDRESS;
            }
        #endif /* __MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__ */
        }
    #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 

        if (!i)
        {
            mmi_display_popup((UI_string_type)GetString(STR_NO_NUMBER_TO_DIAL), MMI_EVENT_FAILURE);
        }
        else if (i == 1)
        {
            g_phb_cntx.active_index_third = 0;
        #if defined(MBM_IMIBASE_SUPPORT)
            if(mbm_sys_is_in_imiworld())
            {
                mbm_phb_pre_send_data();
            }
            else
            {
                mmi_phb_pre_send_data();
            }
        #else//MBM_IMIBASE_SUPPORT
            mmi_phb_pre_send_data();
        #endif//MBM_IMIBASE_SUPPORT
        }
        else
        {
            EntryNewScreen(
                SCR_ID_PHB_MMS_CHOOSE_ENTRY,
                mmi_phb_exit_mms_choose_number,
                mmi_phb_entry_choose_number,
                NULL);
            guiBuffer = GetCurrGuiBuffer(SCR_ID_PHB_MMS_CHOOSE_ENTRY);
            RegisterHighlightHandler(mmi_phb_get_index_third_level);
            
            if (mmi_ucs2strlen((S8*) PhoneBook[store_index].alpha_id.name))
            {
                mmi_ucs2ncpy((S8*)title_string, (S8*) PhoneBook[store_index].alpha_id.name, MAX_SUBMENU_CHARACTERS);
            }
            else
            {
                mmi_ucs2cpy((S8*)title_string, GetString(STR_ID_PHB_UNNAMED));
            }

            ShowCategory353Screen(
                title_string,
                IMG_SCR_PBOOK_CAPTION,
                STR_GLOBAL_OK,
                IMG_GLOBAL_OK,
                STR_GLOBAL_BACK,
                IMG_GLOBAL_BACK,
                i,
                g_phb_cntx.number_to_dial,
                numberTypeImageList,
                NULL,
                0,
                0,
                guiBuffer);
        #if defined(MBM_IMIBASE_SUPPORT)
            if(mbm_sys_is_in_imiworld())
            {
                SetLeftSoftkeyFunction(mbm_phb_pre_send_data, KEY_EVENT_UP);
            }
            else
            {
                SetLeftSoftkeyFunction(mmi_phb_pre_send_data, KEY_EVENT_UP);
            }
        #else//MBM_IMIBASE_SUPPORT
            SetLeftSoftkeyFunction(mmi_phb_pre_send_data, KEY_EVENT_UP);
        #endif//MBM_IMIBASE_SUPPORT
            SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
        }
    }
}


#if defined(__MMI_PHB_INFO_FIELD__) && defined(__MMI_MMS_POSTCARD__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_return_address_data
 * DESCRIPTION
 *  Entry Choose entry screen if there are more then one entry with number field.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_return_address_data(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
//    S16 pError;
    U16 store_index;
//    MMI_PHB_FIELDS_STRUCT *phb_data;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    store_index = g_phb_cntx.active_index_second;
    phb_list_view_get_index_call_back(store_index);
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
}
#endif /* defined(__MMI_PHB_INFO_FIELD__) && defined(__MMI_MMS_POSTCARD__) */


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_list_entry_for_number_and_email
 * DESCRIPTION
 *  This function provide call back function before enter phonebook list
 * PARAMETERS
 *  
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_list_entry_for_number_and_email(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;
    U16 EntryCount = 0;
    U16 store_index;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_LIST_ENTRY_FOR_NUMBER_AND_EMAIL);

    if (mmi_phb_check_ready(MMI_TRUE))
    {
        /* Allocate buffer for phonebook memeber list filter */
        mmi_phb_list_allocate_filter();

        /* Build list according to filter */
        for (i = 0; i < PhoneBookEntryCount; i++)
        {
            store_index = g_phb_name_index[i];
            if ((phb_list_view_type == MMI_PHB_ENTRY_FIELD_NAME || PhoneBook[store_index].field & phb_list_view_type)
                && mmi_phb_check_store_index_by_storage(store_index, g_phb_cntx.prefer_storage))
            {
                g_phb_cntx.list_filter[EntryCount] = store_index;
                EntryCount++;
            }
        }

        if (EntryCount)
        {
            g_phb_cntx.highlight_entry = 0;
            phbListView = MMI_PHB_LIST_FOR_OTHER_APP;
        #if defined(__MMI_PHB_INFO_FIELD__) && defined(__MMI_MMS_POSTCARD__)
            if (phb_list_view_type & MMI_PHB_ENTRY_FIELD_ADDRESS)
            {
                mmi_phb_entry_list(
                    EntryCount,                                 /* Total Entry */
                    STR_SCR_PBOOK_VIEW_CAPTION,                 /* Title String */
                    IMG_SCR_PBOOK_CAPTION,                      /* Title Image */
                    STR_GLOBAL_OK,                              /* LSK */
                    IMG_GLOBAL_OK,                              /* LSK */
                    mmi_phb_get_index_by_store_location_second, /* Highlight Callback */
                    mmi_phb_return_address_data,            /* LSK Callback */
                    NULL,            /* SEND Key Callback */
                    mmi_phb_mms_email_list_get_item,            /* List Callback */
                    mmi_phb_mms_address_list_get_hint,            /* Hint Callback */
                    mmi_phb_list_entry_for_number_and_email, /* Re-Entry Callback */
                    TRUE,                                       /* Alpha Index */
                    TRUE);   /* Right Arrow Key */
            }
            else
        #endif /* defined(__MMI_PHB_INFO_FIELD__) && defined(__MMI_MMS_POSTCARD__) */
            {
                mmi_phb_entry_list(
                    EntryCount,                                 /* Total Entry */
                    STR_SCR_PBOOK_VIEW_CAPTION,                 /* Title String */
                    IMG_SCR_PBOOK_CAPTION,                      /* Title Image */
                    STR_GLOBAL_OK,                              /* LSK */
                    IMG_GLOBAL_OK,                              /* LSK */
                    mmi_phb_get_index_by_store_location_second, /* Highlight Callback */
                    mmi_phb_entry_choose_number,            /* LSK Callback */
                    mmi_phb_entry_choose_number,            /* SEND Key Callback */
                    mmi_phb_mms_email_list_get_item,            /* List Callback */
                    mmi_phb_mms_email_list_get_hint,            /* Hint Callback */
                    mmi_phb_list_entry_for_number_and_email, /* Re-Entry Callback */
                    TRUE,                                       /* Alpha Index */
                    TRUE);   /* Right Arrow Key */
            }         
        }
        else
        {
            mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_NO_ENTRY_TO_SELECT), MMI_EVENT_FAILURE);
            mmi_phb_list_free_filter();
        }
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_list_pre_entry_for_number_and_email
 * DESCRIPTION
 *  This function provide call back function before enter phonebook list
 * PARAMETERS
 *  filter_type        [IN]        filter type
 *  list_view_call_back    [IN]        call back function after user chooses an entry
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_list_pre_entry_for_number_and_email(U16 filter_type, mmi_phb_get_data_callback_type list_view_call_back)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phb_list_view_type = filter_type;
    phbListView = MMI_PHB_LIST_FOR_OTHER_APP;
    phb_list_view_call_back = list_view_call_back;
#ifdef __MMI_PHB_QUICK_SEARCH__
    if (IsScreenPresent(SCR_ID_PHB_GENERIC_QUICK_SEARCH_LIST))
    {
        DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_QUICK_SEARCH_LIST);
    }
    mmi_phb_entry_generic_quick_search_list();
#else
    mmi_phb_list_entry_for_number_and_email();
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_list_pre_entry_for_phb_data_index
 * DESCRIPTION
 *  This function provide call back function before enter phonebook list
 * PARAMETERS
 *  filter_type        [IN]        filter type
 *  list_view_call_back    [IN]        call back function after user chooses an entry
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_list_pre_entry_for_phb_data_index(U16 filter_type, mmi_phb_get_index_callback_type get_index_call_back)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phb_list_view_type = filter_type;
    phbListView = MMI_PHB_LIST_FOR_OTHER_APP;
    phb_list_view_get_index_call_back = get_index_call_back;
#ifdef __MMI_PHB_QUICK_SEARCH__
    if (IsScreenPresent(SCR_ID_PHB_GENERIC_QUICK_SEARCH_LIST))
    {
        DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_QUICK_SEARCH_LIST);
    }
    mmi_phb_entry_generic_quick_search_list();
#else
    mmi_phb_list_entry_for_number_and_email();
#endif
}


/*------------------------------------------------------  Interface for MMS  ------------------------------------------------------*/
#define MMI_PHB_INTERFACE_FOR_MMS
#if defined(MMS_SUPPORT)
/*****************************************************************************
 * FUNCTION
 *  EntryPhbFromMMSNumber
 * DESCRIPTION
 *  Entry phonebook list from MMS, list entry with number field only
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void EntryPhbFromMMSNumber(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_list_pre_entry_for_number_and_email(MMI_PHB_ENTRY_FIELD_ALL_NUMBER, mmi_phb_mms_send_data_to_mms);
}


/*****************************************************************************
 * FUNCTION
 *  EntryPhbFromMMSEmail
 * DESCRIPTION
 *  Entry phonebook list from MMS, list entry with email field only
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void EntryPhbFromMMSEmail(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_list_pre_entry_for_number_and_email(MMI_PHB_ENTRY_FIELD_EMAIL, mmi_phb_mms_send_data_to_mms);
}


/*****************************************************************************
 * FUNCTION
 *  EntryPhbFromMMSAll
 * DESCRIPTION
 *  Entry phonebook list from MMS, list all entry
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void EntryPhbFromMMSAll(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phbListView = MMI_PHB_LIST_FOR_MMS_ALL;
    mmi_phb_list_pre_entry_second_level();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_mms_choose_number
 * DESCRIPTION
 *  Entry Choose entry screen if there are more then one entry with number field.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_mms_choose_number(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;
    U8 *guiBuffer;
    U16 numberTypeImageList[5];
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)    
    S16 pError;
#endif
    U16 store_index;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    store_index = g_phb_name_index[g_phb_cntx.active_index_second];

    mmi_ucs2cpy(pbName, (PS8) PhoneBook[store_index].alpha_id.name);
    mmi_phb_convert_get_ucs2_number(pbNumber, store_index); /* BCD number format. */

#ifndef __MMI_PHB_USIM_FIELD__
    if (store_index >= MAX_PB_PHONE_ENTRIES)   /* In SIM */
    {
        if (!mmi_ucs2strlen(pbNumber))
        {
            mmi_display_popup((UI_string_type)GetString(STR_NO_NUMBER_TO_DIAL), MMI_EVENT_FAILURE);
        }
        else
        {
            g_phb_cntx.number_to_dial[0] = (U8*) pbNumber;
            g_phb_cntx.active_index_third = 0;
            mmi_phb_mms_pre_send_data_to_mms();
        }
    }
    else
#endif /* __MMI_PHB_USIM_FIELD__ */ 
    {
        i = 0;

        if (mmi_ucs2strlen(pbNumber))
        {
            g_phb_cntx.number_to_dial[i] = (U8*) pbNumber;
            TitleStrings[i] = STR_ID_PHB_MOBILE_NUMBER;
            numberTypeImageList[i++] = IMG_MOBILE_NUMBER;
        }
    #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
    #ifdef __MMI_PHB_USIM_FIELD__
        if (store_index >= MAX_PB_PHONE_ENTRIES)
        {
            mmi_phb_op_set_option_data_by_usim_index((U16) store_index, &PhoneBookOptionalFields);
        }
        else
    #endif /* __MMI_PHB_USIM_FIELD__ */ 
        {
            ReadRecord(
                NVRAM_EF_PHB_FIELDS_LID,
                (U16) (store_index + 1),
                (void*)&PhoneBookOptionalFields,
                OPTIONAL_FIELDS_RECORD_SIZE,
                &pError);
        }
        /* All Number */
        mmi_asc_to_ucs2(pbHomeNumber, (PS8) PhoneBookOptionalFields.homeNumber);
        if (mmi_ucs2strlen(pbHomeNumber))
        {
            g_phb_cntx.number_to_dial[i] = (PU8) pbHomeNumber;
            TitleStrings[i] = STR_HOME_NUMBER;
            numberTypeImageList[i++] = IMG_HOME_NUMBER;
        }
        mmi_asc_to_ucs2(pbOfficeNumber, (PS8) PhoneBookOptionalFields.officeNumber);
        if (mmi_ucs2strlen(pbOfficeNumber))
        {
            g_phb_cntx.number_to_dial[i] = (PU8) pbOfficeNumber;
            TitleStrings[i] = STR_OFFICE_NUMBER;
            numberTypeImageList[i++] = IMG_OFFICE_NUMBER;
        }
        mmi_asc_to_ucs2(pbFaxNumber, (PS8) PhoneBookOptionalFields.faxNumber);
        if (mmi_ucs2strlen(pbFaxNumber))
        {
            g_phb_cntx.number_to_dial[i] = (PU8) pbFaxNumber;
            TitleStrings[i] = STR_FAX_NUMBER;
            numberTypeImageList[i++] = IMG_FAX_NUMBER;
        }

        /* Email */
        mmi_asc_to_ucs2(pbEmailAddress, (PS8) PhoneBookOptionalFields.emailAddress);
        if (mmi_ucs2strlen(pbEmailAddress))
        {
            g_phb_cntx.number_to_dial[i] = (PU8) pbEmailAddress;
            TitleStrings[i] = mmi_phb_get_email_address_id(1);
            numberTypeImageList[i++] = IMG_EMAIL_ADDRESS;
        }
    #ifdef __MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__
        mmi_asc_to_ucs2(pbEmailAddress2, (PS8) PhoneBookOptionalFields.emailAddress2);
        if (mmi_ucs2strlen(pbEmailAddress2))
        {
            g_phb_cntx.number_to_dial[i] = (PU8) pbEmailAddress2;
            TitleStrings[i] = STR_ID_PHB_EMAIL2_ADRESS;
            numberTypeImageList[i++] = IMG_EMAIL_ADDRESS;
        }
    #endif
    #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 

        if (!i)
        {
            mmi_display_popup((UI_string_type)GetString(STR_NO_NUMBER_TO_DIAL), MMI_EVENT_FAILURE);
        }
        else if (i == 1)
        {
            g_phb_cntx.active_index_third = 0;
            mmi_phb_mms_pre_send_data_to_mms();
        }
        else
        {
            EntryNewScreen(
                SCR_ID_PHB_MMS_CHOOSE_ENTRY,
                mmi_phb_exit_mms_choose_number,
                mmi_phb_entry_mms_choose_number,
                NULL);
            guiBuffer = GetCurrGuiBuffer(SCR_ID_PHB_MMS_CHOOSE_ENTRY);
            RegisterHighlightHandler(mmi_phb_get_index_third_level);
            ShowCategory53Screen(
                STR_CHOOSE_NUMBER_CAPTION,
                IMG_SCR_PBOOK_CAPTION,
                STR_GLOBAL_OK,
                IMG_GLOBAL_OK,
                STR_GLOBAL_BACK,
                IMG_GLOBAL_BACK,
                i,
                g_phb_cntx.number_to_dial,
                numberTypeImageList,
                NULL,
                0,
                0,
                guiBuffer);

            SetLeftSoftkeyFunction(mmi_phb_mms_pre_send_data_to_mms, KEY_EVENT_UP);
            SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
        }
    }
}



/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mms_pre_send_data_to_mms
 * DESCRIPTION
 *  Send choosed number to MMS
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_mms_pre_send_data_to_mms(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_mms_send_data_to_mms(pbName, (S8*) g_phb_cntx.number_to_dial[g_phb_cntx.active_index_third]);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mms_send_data_to_mms
 * DESCRIPTION
 *  send entry name and choosed number to MMS by sending primitives
 * PARAMETERS
 *  name        [IN]        Name field to be sent
 *  number      [IN]        Number field to be sent.
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_mms_send_data_to_mms(S8 *name, S8 *number)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_MMS_SEND_DATA_TO_MMS);    
    ClearInputEventHandler(MMI_DEVICE_ALL); /* Clear Key Handler to avoid resend message. */
#if defined(OBIGO_Q03C_MMS_V01)
    widget_MMI_fullscreen_editor_update_from_phonebook((kal_uint8*) number);
#endif 
#if defined (JATAAYU_SUPPORT)
    if (phb_list_view_type == MMI_PHB_ENTRY_FIELD_ALL_NUMBER)
    {
        update_mms_number_from_phonebook(number);
    }
    else if (phb_list_view_type == MMI_PHB_ENTRY_FIELD_EMAIL)
    {
        update_mms_email_from_phonebook(number);
    }
#endif /* JATAAYU_SUPPORT */ 
}
#endif /* defined(MMS_SUPPORT) */


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_exit_mms_choose_number
 * DESCRIPTION
 *  exit function
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_exit_mms_choose_number(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.end_scr_id = SCR_ID_PHB_MMS_CHOOSE_ENTRY;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mms_email_list_get_item
 * DESCRIPTION
 *  dynamic list call back function
 * PARAMETERS
 *  item_index          [IN]        
 *  str_buff            [IN]        
 *  img_buff_p          [?]         
 *  str_img_mask        [IN]        
 * RETURNS
 *  
 *****************************************************************************/
pBOOL mmi_phb_mms_email_list_get_item(S32 item_index, UI_string_type str_buff, PU8 *img_buff_p, U8 str_img_mask)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    i = g_phb_cntx.list_filter[item_index];

    if (mmi_ucs2strlen((S8*) PhoneBook[i].alpha_id.name))
    {
        mmi_ucs2ncpy((S8*) str_buff, (S8*) PhoneBook[i].alpha_id.name, MAX_SUBMENU_CHARACTERS);
    }
    else
    {
        mmi_phb_convert_get_ucs2_number((S8*) str_buff, i);
    }

#ifdef __MMI_DUAL_SIM_MASTER__
    if(i >= (MAX_PB_PHONE_ENTRIES+MAX_PB_SIM_ENTRIES))
    {
        *img_buff_p = get_image(IMG_ID_CARD2_PHB_STORAGE_SIM);
    }
    else if (i >= MAX_PB_PHONE_ENTRIES)
    {
        *img_buff_p = get_image(IMG_ID_CARD1_PHB_STORAGE_SIM);
    }
    else
    {
        *img_buff_p = get_image(IMG_STORAGE_HANDSET);
    }
#else /* __MMI_DUAL_SIM_MASTER__ */
    if (i >= MAX_PB_PHONE_ENTRIES)
    {
        *img_buff_p = get_image(IMG_STORAGE_SIM);
    }
    else
    {
        *img_buff_p = get_image(IMG_STORAGE_HANDSET);
    }
#endif /* __MMI_DUAL_SIM_MASTER__ */

    return TRUE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mms_email_list_get_hint
 * DESCRIPTION
 *  
 * PARAMETERS
 *  item_index      [IN]        
 *  hint_array      [?]         
 * RETURNS
 *  
 *****************************************************************************/
S32 mmi_phb_mms_email_list_get_hint(S32 item_index, UI_string_type *hint_array)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index;
    S8 temp_number[(MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    store_index = g_phb_cntx.list_filter[item_index];

    mmi_phb_convert_get_ucs2_number((S8*) temp_number, store_index);

    if (mmi_ucs2strlen((S8*) PhoneBook[store_index].alpha_id.name) && mmi_ucs2strlen((S8*) temp_number))
    {
        mmi_ucs2ncpy((S8*) hint_array[0], (S8*) temp_number, MAX_SUBMENU_CHARACTERS);
    }
    else
    {
        return 0;   /* No Hint Data */
    }
    return 1;   /* One hint data only, can be more hints. */
}


#if defined(__MMI_PHB_INFO_FIELD__) && defined(__MMI_MMS_POSTCARD__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_mms_address_list_get_hint
 * DESCRIPTION
 *  
 * PARAMETERS
 *  item_index      [IN]        
 *  hint_array      [?]         
 * RETURNS
 *  
 *****************************************************************************/
S32 mmi_phb_mms_address_list_get_hint(S32 item_index, UI_string_type *hint_array)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index;
    S16 pError;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    store_index = g_phb_cntx.list_filter[item_index];

    ReadRecord(
        NVRAM_EF_PHB_INFO_LID,
        (U16) (store_index + 1),
        (void*)&PhoneBookInfoFields,
        MMI_PHB_INFO_RECORD_SIZE,
        &pError);
    
    if (mmi_ucs2strlen((S8*) PhoneBookInfoFields.address.street))
    {
        mmi_ucs2cpy((S8*) hint_array[0], (S8*) PhoneBookInfoFields.address.street);
    }
    else
    {
        return 0;   /* No Hint Data */
    }
    return 1;   /* One hint data only, can be more hints. */
}
#endif /* defined(__MMI_PHB_INFO_FIELD__) && defined(__MMI_MMS_POSTCARD__) */
/*--------------------------------------------------END Interface for MMS  ------------------------------------------------------*/


/*------------------------------------------------------  Interface for File Manager Image---------------------------------------------------*/
#define MMI_PHB_INTERFACE_FOR_FMGR
#if defined(__MMI_FILE_MANAGER__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_fmgr_select_image_done
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_phb_fmgr_select_image_done(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_entry_fmgr_save_option();
    DeleteScreenIfPresent(SCR_ID_PHB_IMAGE_VIEW_IMAGE);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_exit_fmgr_image_preview
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_phb_exit_fmgr_image_preview(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.end_scr_id = SCR_ID_PHB_IMAGE_VIEW_IMAGE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_fmgr_image_preview
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_phb_fmgr_image_preview(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    EntryNewScreen(SCR_ID_PHB_IMAGE_VIEW_IMAGE, mmi_phb_exit_fmgr_image_preview, mmi_phb_fmgr_image_preview, NULL);

    /* entry cat222 to decode and display a image from file */
    wgui_cat222_disable_err_msg_display();

    ShowCategory222Screen(
        STR_GLOBAL_VIEW,
        IMG_SCR_PBOOK_CAPTION,
        STR_GLOBAL_OK,
        IMG_GLOBAL_OK,
        STR_GLOBAL_BACK,
        IMG_GLOBAL_BACK,
        GDI_COLOR_BLACK,
        (PS8) NULL,
        (PS8) g_phb_file_path,
        FALSE,
        mmi_phb_image_preview_callback,
        GDI_IMAGE_SRC_FROM_FILE);

    SetLeftSoftkeyFunction(mmi_phb_fmgr_select_image_done, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
}


#if defined(__MMI_INCOMING_CALL_VIDEO__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_fmgr_select_video_done
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_phb_fmgr_select_video_done(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_entry_fmgr_save_option();
    DeleteScreenIfPresent(SCR_ID_PHB_VIDEO_PREVIEW);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_exit_fmgr_video_preview
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_phb_exit_fmgr_video_preview(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.end_scr_id = SCR_ID_PHB_VIDEO_PREVIEW;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_fmgr_video_preview
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_phb_fmgr_video_preview(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    PU8 gui_buffer;
    MMI_BOOL is_play_audio = (MMI_BOOL) !IsSilentModeActivated();

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (isInCall() && !GetWapCallPresent()) /* Can not preview video during call. */
    {
    #if defined(__MMI_BACKGROUND_CALL__)
        if (CheckIsBackHistory())
        {
            EntryNewScreen(SCR_ID_PHB_VIDEO_PREVIEW, mmi_phb_exit_fmgr_video_preview, mmi_phb_fmgr_video_preview, NULL);
            gui_buffer = GetCurrGuiBuffer(SCR_ID_PHB_VIDEO_PREVIEW);
            
            ShowCategory141Screen(
                STR_GLOBAL_PLAY,
                GetRootTitleIcon(MAIN_MENU_PHONEBOOK_MENUID),
                0,
                0,
                STR_GLOBAL_BACK,
                IMG_GLOBAL_BACK,
                (U8*)GetString(STR_GLOBAL_CURRENTLY_NOT_AVAILABLE_IN_CALL),
                0,
                gui_buffer);
            SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
            return;
        }
        else
    #endif
        {
            mmi_ucm_app_entry_error_message();    
            return;
        }
    }

    EntryNewScreen(SCR_ID_PHB_VIDEO_PREVIEW, mmi_phb_exit_fmgr_video_preview, mmi_phb_fmgr_video_preview, NULL);
    gui_buffer = GetCurrGuiBuffer(SCR_ID_PHB_VIDEO_PREVIEW);

#ifdef __MMI_SWFLASH__    
    if(g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_SWFLASH)
    {
        ShowCategory229Screen(
            (U8*)GetString(STR_GLOBAL_PLAY),         /* title_str */
            IMG_SCR_PBOOK_CAPTION,                              /* title_icon */
            STR_GLOBAL_OK,                                      /* lsk_str */
            IMG_GLOBAL_OK,                                      /* lsk_icon */
            STR_GLOBAL_BACK,                                    /* rsk_str */
            IMG_GLOBAL_BACK,                                    /* rsk_icon */
            0,                                                  /* video_id */
            (S8*) g_phb_video_file_path,                        /* video_filename */
            1,                                                  /* repeat_count */
            is_play_audio,                   /* is_play_audio */
            TRUE,                                               /* is_vibrate_on */
            FALSE,                                              /* is_interaction_on */
            TRUE,                                               /* is_lcd_no_sleep */
            0,  							                    /* is_full_screen */
            GDI_COLOR_WHITE,                                    /* bg_color */
            mmi_phb_video_view_callback,
            gui_buffer);

        SetLeftSoftkeyFunction(mmi_phb_fmgr_select_video_done, KEY_EVENT_UP);
        SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
    }
    else
#endif /* __MMI_SWFLASH__ */
    {
    #ifdef __DRM_SUPPORT__
        S32 drm_video_flag = WGUI_CATVDO_DRM_DISABLE_CONSUME_TIME | WGUI_CATVDO_DRM_DISABLE_CONSUME_COUNT | WGUI_CATVDO_DRM_DISABLE_CONSUME_TIME_LIMIT_STOP;
        wgui_catvdo_set_drm_flag(drm_video_flag);
    #endif
        ShowCategory225Screen(
            STR_GLOBAL_PLAY,         /* title_str */
            IMG_SCR_PBOOK_CAPTION,              /* title_icon */
            STR_GLOBAL_OK,                      /* lsk_str */
            IMG_GLOBAL_OK,                      /* lsk_icon */
            STR_GLOBAL_BACK,                    /* rsk_str */
            IMG_GLOBAL_BACK,                    /* rsk_icon */
            0,                                  /* video_id */
            (S8*) g_phb_video_file_path,        /* video_filename */
            1,                                  /* repeat_count */
            TRUE,                               /* is_visual_update */
            is_play_audio,   /* is_play_audio */
            TRUE,                               /* is_lcd_no_sleep */
            GDI_COLOR_WHITE,                    /* bg_color */
            mmi_phb_video_view_callback,
            gui_buffer);
    
        SetLeftSoftkeyFunction(mmi_phb_fmgr_select_video_done, KEY_EVENT_UP);
        SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
    }
}
#endif /* __MMI_INCOMING_CALL_VIDEO__ */


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_fmgr_from_file_mgr
 * DESCRIPTION
 *  forward function for file manager application
 * PARAMETERS
 *  path        [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_fmgr_from_file_mgr(S8 *path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
#if defined(__MMI_INCOMING_CALL_VIDEO__)
    FMGR_FILTER file_type;
#endif /* defined(__MMI_INCOMING_CALL_VIDEO__) */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __SYNCML_SUPPORT__
    if (mmi_syncml_is_phb_sync_now())
    {
        mmi_phb_entry_not_ready(STR_ID_SYNC_PLEASE_WAIT);
        return;
    }
#endif /* __SYNCML_SUPPORT__ */
    if (path == NULL)
    {
        mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_FMGR_IMAGE_NO_SELECT), MMI_EVENT_FAILURE);
        return;
    }

#if defined(__MMI_INCOMING_CALL_VIDEO__)
    /* check video file type */
    mmi_fmgr_get_file_type((PS8) path, &file_type);
    if ((FMGR_FILTER_IS_SET(&file_type, FMGR_TYPE_3GP)) || (FMGR_FILTER_IS_SET(&file_type, FMGR_TYPE_MP4)) ||
        (FMGR_FILTER_IS_SET(&file_type, FMGR_TYPE_AVI)) || (FMGR_FILTER_IS_SET(&file_type, FMGR_TYPE_3G2)) ||
        (FMGR_FILTER_IS_SET(&file_type, FMGR_TYPE_MPG)))
    {
        if (mmi_phb_video_check_data_space(path))
        {
            mmi_ucs2cpy(g_phb_video_file_path, path);
            g_phb_cntx.recv_res_from = MMI_PHB_RECV_RES_VIDEO;
            mmi_phb_fmgr_video_preview();
        }
    }
#if defined(__MMI_SWFLASH__)
    else if ((FMGR_FILTER_IS_SET(&file_type, FMGR_TYPE_VIS)) ||
        (FMGR_FILTER_IS_SET(&file_type, FMGR_TYPE_IVIS)) || (FMGR_FILTER_IS_SET(&file_type, FMGR_TYPE_KVIS)) ||
        (FMGR_FILTER_IS_SET(&file_type, FMGR_TYPE_MVIS)))
    {
        if (mmi_phb_video_check_data_space(path))
        {
            mmi_ucs2cpy(g_phb_video_file_path, path);
            g_phb_cntx.recv_res_from = MMI_PHB_RECV_RES_SWFLASH;
            mmi_phb_fmgr_video_preview();
        }
    }
#endif /* defined(__MMI_SWFLASH__) */ 
    /* Check image content */
    else if (mmi_phb_image_check_valid_image(path))
#else /* defined(__MMI_INCOMING_CALL_VIDEO__) */ 

    /* Check image content */
    if (mmi_phb_image_check_valid_image(path))
#endif /* defined(__MMI_INCOMING_CALL_VIDEO__) */ 
    {
        mmi_ucs2cpy(g_phb_file_path, path);
        g_phb_cntx.recv_res_from = MMI_PHB_RECV_RES_IMAGE;
        mmi_phb_fmgr_image_preview();
    }

    /* need to add error handler */

    /* Clear Previous PHB screen, and set phb new entry point here. */
    mmi_phb_clear_old_history();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_highlight_fmgr_add_entry
 * DESCRIPTION
 *  Highlight function to associate image from file manager to a new phonebook entry
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_highlight_fmgr_add_entry(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SetLeftSoftkeyFunction(mmi_phb_fmgr_pre_add_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
    SetKeyHandler(mmi_phb_fmgr_pre_add_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_highlight_fmgr_edit_entry
 * DESCRIPTION
 *  Highlight function for associate image to a existing phonebook entry
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_highlight_fmgr_edit_entry(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SetLeftSoftkeyFunction(mmi_phb_list_pre_entry_for_fmgr_in_nvram, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
    SetKeyHandler(mmi_phb_list_pre_entry_for_fmgr_in_nvram, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);

    phbListView = MMI_PHB_LIST_FOR_SAVE_FROM_FILE_MGR;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_highlight_edit_callergroup
 * DESCRIPTION
 *  Highlight function for associate image to a existing phonebook caller group
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_highlight_edit_callergroup(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SetLeftSoftkeyFunction(mmi_phb_entry_callergroup, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
    SetKeyHandler(mmi_phb_entry_callergroup, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);

    phbListView = MMI_PHB_LIST_FOR_SAVE_FROM_FILE_MGR;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_fmgr_save_option
 * DESCRIPTION
 *  Entry function for save image from file manager option menu
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_fmgr_save_option(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 *guiBuffer;
    U16 nStrItemList[3];
    U16 nNumofItem;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    EntryNewScreen(SCR_ID_PHB_FMGR_SAVE_OPTION, mmi_phb_exit_fmgr_save_option, mmi_phb_entry_fmgr_save_option, NULL);
    guiBuffer = GetCurrGuiBuffer(SCR_ID_PHB_FMGR_SAVE_OPTION);
    nNumofItem = GetNumOfChild(MENU_ID_PHB_FMGR_SAVE_OPTION);
    GetSequenceStringIds(MENU_ID_PHB_FMGR_SAVE_OPTION, nStrItemList);
    SetParentHandler(MENU_ID_PHB_FMGR_SAVE_OPTION);
    RegisterHighlightHandler(ExecuteCurrHiliteHandler);
    ShowCategory15Screen(
        STR_GLOBAL_OPTIONS,
        IMG_SCR_PBOOK_CAPTION,
        STR_GLOBAL_OK,
        IMG_GLOBAL_OK,
        STR_GLOBAL_BACK,
        IMG_GLOBAL_BACK,
        nNumofItem,
        nStrItemList,
        (U16*) gIndexIconsImageList,
        LIST_MENU,
        0,
        guiBuffer);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_exit_fmgr_save_option
 * DESCRIPTION
 *  Exit function
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_exit_fmgr_save_option(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.end_scr_id = SCR_ID_PHB_FMGR_SAVE_OPTION;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_fmgr_pre_add_entry
 * DESCRIPTION
 *  pre check for associate image from file manager to a new phonebook entry
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_fmgr_pre_add_entry(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __SYNCML_SUPPORT__
    if (mmi_syncml_is_phb_sync_now())
    {
        mmi_phb_entry_not_ready(STR_ID_SYNC_PLEASE_WAIT);
    }
    else
#endif /* __SYNCML_SUPPORT__ */
#ifdef __MMI_DUAL_SIM_MASTER__
    if (MTPNP_PFAL_Phb_IsReady() == MTPNP_TRUE)
#else /* __MMI_DUAL_SIM_MASTER__ */
    if (g_phb_cntx.phb_ready && !g_phb_cntx.processing)
#endif /* __MMI_DUAL_SIM_MASTER__ */
    {
        if (g_phb_cntx.phone_used >= g_phb_cntx.phone_total)    /* No spcace in NVRAM */
        {
            mmi_display_popup((UI_string_type)GetString(STR_PBOOK_FULL_MSG), MMI_EVENT_FAILURE);  
        }
        else
        {
            mmi_phb_util_clear_buffer(TRUE);

            if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_IMAGE)
            {
                mmi_phb_build_image_list();
                g_phb_cntx.selected_pic_index = g_phb_cntx.total_image_id - 1;
                g_phb_cntx.image_location = MMI_PHB_IMAGE_SELECT_PATH;
            }
        #if defined(__MMI_AVATAR__)
            else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_IMAGE)
            {
                mmi_phb_build_image_list();
                g_phb_cntx.selected_pic_index = g_phb_cntx.total_image_id - 2;
                g_phb_cntx.image_location = MMI_PHB_AVATAR_SELECT_PATH;
            }
        #endif /* defined(__MMI_AVATAR__) */			
            else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AUDIO)
            {
                mmi_phb_build_ring_tone_list();
                g_phb_cntx.selected_ring_index = mmi_phb_get_ring_tone_index(g_phb_cntx.recv_res_id);
            }
        #if defined(__MMI_INCOMING_CALL_VIDEO__)
            else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_VIDEO)
            {
                mmi_phb_build_video_list();
                g_phb_cntx.selected_video_index = g_phb_cntx.total_video_id - 1;
                g_phb_cntx.video_location = MMI_PHB_IMAGE_SELECT_PATH;
            }
        #ifdef __MMI_AVATAR__
            else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_VIDEO)
            {
                mmi_phb_build_video_list();
                g_phb_cntx.selected_video_index = g_phb_cntx.total_video_id - g_phb_avatar_video_minus;
                g_phb_cntx.video_location = MMI_PHB_AVATAR_SELECT_PATH;
            }
        #endif /* __MMI_AVATAR__ */ 
        #ifdef __MMI_SWFLASH__
            else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_SWFLASH)
            {
                mmi_phb_build_video_list();
                g_phb_cntx.selected_video_index = g_phb_cntx.total_video_id - g_phb_swflash_video_minus;
                g_phb_cntx.video_location = MMI_PHB_SWFLASH_SELECT_PATH;
            }
        #endif /* __MMI_SWFLASH__ */ 
        #endif /* defined(__MMI_INCOMING_CALL_VIDEO__) */ 

            g_phb_cntx.set_done_flag = 2;
            g_phb_cntx.selected_storage = MMI_NVRAM;
        #ifdef __MMI_PHB_SIM_NUMBER_MANAGEMENT__
            g_phb_add_more_op = MMI_PHB_OP_NONE;
        #endif
            mmi_phb_entry_op_add_entry();
            DeleteScreenIfPresent(SCR_ID_PHB_FMGR_SAVE_OPTION);
        }
    }
    else
    {
        mmi_phb_entry_not_ready(STR_PROCESSING_PHONEBOOK);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_fmgr_pre_edit_entry
 * DESCRIPTION
 *  pre check for associate image to a existing phonebook entry
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_fmgr_pre_edit_entry(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i, store_index;
    S16 pError;
    U8 optid_record;
    U8 optid_index;
    PHB_OPTIONAL_IDS_STRUCT PhbOptIDs[OPTIONAL_IDS_RECORD_TOTAL];
    BOOL result = FALSE;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    store_index = g_phb_cntx.active_index;

    if (store_index >= MAX_PB_PHONE_ENTRIES)    /* ERROR!, Record in NVRAM only */
    {
        /* Print Error Log Here. */
        GoBackHistory();
        return;
    }

    /* set active index for edit entry */
    for (i = 0; i < PhoneBookEntryCount; i++)
    {
        if (g_phb_name_index[i] == store_index)
        {
            g_phb_cntx.active_index = i;
            break;
        }
    }

    if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_IMAGE
        || g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_IMAGE)
    {
        mmi_phb_build_image_list();
        
        /* Read out related optional id record */
        optid_record = (store_index / OPTIONAL_IDS_RECORD_TOTAL) + 1;
        optid_index = store_index - (optid_record - 1) * OPTIONAL_IDS_RECORD_TOTAL;
        ReadRecord(NVRAM_EF_PHB_IDS_LID, optid_record, (void*)PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);

        /* Image setting */
        if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_IMAGE)
        {
            g_phb_cntx.selected_pic_index = g_phb_cntx.total_image_id - 1;
            g_phb_cntx.image_location = MMI_PHB_IMAGE_SELECT_PATH;
            g_phb_image_type_id = 1;
        }
    #if defined(__MMI_AVATAR__)
        else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_IMAGE)
        {
            g_phb_cntx.selected_pic_index = g_phb_cntx.total_image_id - 2;
            g_phb_cntx.image_location = MMI_PHB_AVATAR_SELECT_PATH;
            g_phb_image_type_id = 2;
        }
    #endif /* defined(__MMI_AVATAR__) */
    #if defined(__MMI_FILE_MANAGER__) && !defined(__MMI_DISPLAY_CHAR_HIDE_MENU_FROM_FILE__)
        result = mmi_phb_image_update_by_index((U16)(store_index + 1));   /* Update from file manager */
    #endif
        if (!result)    /* Update image fail. */
        {
            g_phb_cntx.selected_pic_index = 0;
            PhbOptIDs[optid_index].pictureTagID = g_phb_cntx.image_id_list[g_phb_cntx.selected_pic_index];
            WriteRecord(NVRAM_EF_PHB_IDS_LID, optid_record, PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);        
            mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_OP_IMAGE_FAIL), MMI_EVENT_FAILURE);
        }
        else
        {
            PhbOptIDs[optid_index].pictureTagID = g_phb_image_type_id;

            /* Update associate id and write it back */
            WriteRecord(NVRAM_EF_PHB_IDS_LID, optid_record, PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);        
            mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS);
        }

        g_phb_cntx.selected_pic_index = 0;
        g_phb_cntx.image_location = MMI_PHB_IMAGE_NO_SELECT;
    }
    else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AUDIO)
    {
        mmi_phb_build_ring_tone_list();
        
        /* Read out related optional id record */
        optid_record = (store_index / OPTIONAL_IDS_RECORD_TOTAL) + 1;
        optid_index = store_index - (optid_record - 1) * OPTIONAL_IDS_RECORD_TOTAL;
        ReadRecord(NVRAM_EF_PHB_IDS_LID, optid_record, (void*)PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);
        
        g_phb_cntx.selected_ring_index = mmi_phb_get_ring_tone_index(g_phb_cntx.recv_res_id);
        PhbOptIDs[optid_index].ringToneID = g_phb_cntx.ring_tone_list[g_phb_cntx.selected_ring_index];

        /* Update associate id and write it back */
        WriteRecord(NVRAM_EF_PHB_IDS_LID, optid_record, PhbOptIDs, OPTIONAL_IDS_RECORD_SIZE, &pError);        
        
        mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS);
    }
#if defined(__MMI_INCOMING_CALL_VIDEO__)
    else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_VIDEO
             || g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_VIDEO
             || g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_SWFLASH)
    {
        mmi_phb_build_video_list();
        
        if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_VIDEO)
        {
            g_phb_cntx.selected_video_index = g_phb_cntx.total_video_id - 1;
            g_phb_cntx.video_location = MMI_PHB_IMAGE_SELECT_PATH;
            g_phb_cntx.video_audio = 1; /* Turn on video's audio by default */
        }
    #ifdef __MMI_AVATAR__
        else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_VIDEO)
        {
            g_phb_cntx.selected_video_index = g_phb_cntx.total_video_id - g_phb_avatar_video_minus;
            g_phb_cntx.video_location = MMI_PHB_AVATAR_SELECT_PATH;
            g_phb_cntx.video_audio = 1; /* Turn on video's audio by default */
        }
    #endif /* __MMI_AVATAR__ */ 
    #ifdef __MMI_SWFLASH__
        else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_SWFLASH)
        {
            g_phb_cntx.selected_video_index = g_phb_cntx.total_video_id - g_phb_swflash_video_minus;
            g_phb_cntx.video_location = MMI_PHB_SWFLASH_SELECT_PATH;
            g_phb_cntx.video_audio = 1; /* Turn on video's audio by default */
        }
    #endif /* __MMI_SWFLASH__ */
        
        result = mmi_phb_video_update_by_index(store_index);
        if (!result)    /* Update video fail. */
        {
            mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_OP_VIDEO_FAIL), MMI_EVENT_FAILURE);
        }
        else
        {
            mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS);
        }
        g_phb_cntx.selected_video_index = 0;
        g_phb_cntx.video_location = MMI_PHB_IMAGE_NO_SELECT;
    }
#endif /* __MMI_INCOMING_CALL_VIDEO__ */
    else
    {
        /* error handler */
        mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE);
    }
    
    DeleteScreenIfPresent(SCR_PBOOK_LIST);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_fmgr_pre_edit_callergrp
 * DESCRIPTION
 *  pre check for associate image to a existing phonebook caller group
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_fmgr_pre_edit_callergrp(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    PHB_CALLER_GROUP_STRUCT *callerGroups;
    BOOL result = FALSE;
	S16 pError;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    callerGroups = (PHB_CALLER_GROUP_STRUCT*) g_phb_cntx.caller_group;
    mmi_ucs2cpy(pbName, (PS8) callerGroups[g_phb_cntx.active_index].groupName);

    /* Image setting */
    if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_IMAGE
        || g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_IMAGE)
    {
        mmi_phb_build_image_list();

        if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_IMAGE)
        {
            g_phb_cntx.selected_pic_index = g_phb_cntx.total_image_id - 1;
            g_phb_cntx.image_location = MMI_PHB_IMAGE_SELECT_PATH;
            g_phb_image_type_id = 1;
        }
    #if defined(__MMI_AVATAR__)
        else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_IMAGE)
        {
            g_phb_cntx.selected_pic_index = g_phb_cntx.total_image_id - 2;
            g_phb_cntx.image_location = MMI_PHB_AVATAR_SELECT_PATH;
            g_phb_image_type_id = 2;
        }
    #endif /* defined(__MMI_AVATAR__) */
    #if defined(__MMI_FILE_MANAGER__) && !defined(__MMI_DISPLAY_CHAR_HIDE_MENU_FROM_FILE__)
        result = mmi_phb_image_update_by_index((U16) (g_phb_cntx.active_index + 5001)); /* Copy image to folder in file system, 5001 for group 1 and so on. */
    #endif
        if (!result)    /* Edit image fail. */
        {
            g_phb_cntx.selected_pic_index = 0;
                     
            callerGroups[g_phb_cntx.active_index].pictureTagID = g_phb_cntx.image_id_list[g_phb_cntx.selected_pic_index];
            WriteRecord(NVRAM_EF_PHB_CALLER_GROUPS_LID, 1, callerGroups, CALLER_GROUPS_RECORD_SIZE, &pError);
            mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_OP_IMAGE_FAIL), MMI_EVENT_FAILURE);
        }
        else
        {            
            callerGroups[g_phb_cntx.active_index].pictureTagID = g_phb_image_type_id;
            WriteRecord(NVRAM_EF_PHB_CALLER_GROUPS_LID, 1, callerGroups, CALLER_GROUPS_RECORD_SIZE, &pError);
            
            mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS);
        }

        g_phb_cntx.selected_pic_index = 0;
        g_phb_cntx.image_location = MMI_PHB_IMAGE_NO_SELECT;
    }
    /* audio setting */
    else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AUDIO)
    {
        mmi_phb_build_ring_tone_list();        
        g_phb_cntx.selected_ring_index = mmi_phb_get_ring_tone_index(g_phb_cntx.recv_res_id);
        callerGroups[g_phb_cntx.active_index].ringToneID = g_phb_cntx.ring_tone_list[g_phb_cntx.selected_ring_index];

        WriteRecord(NVRAM_EF_PHB_CALLER_GROUPS_LID, 1, callerGroups, CALLER_GROUPS_RECORD_SIZE, &pError);
        
        mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS);
    }
    /* video setting */
#if defined(__MMI_INCOMING_CALL_VIDEO__)
    else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_VIDEO
             || g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_VIDEO
             || g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_SWFLASH)
    {
        mmi_phb_build_video_list();
        
        if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_VIDEO)
        {
            g_phb_cntx.selected_video_index = g_phb_cntx.total_video_id - 1;
            g_phb_cntx.video_location = MMI_PHB_IMAGE_SELECT_PATH;
            g_phb_cntx.video_audio = 1; /* Turn on video's audio by default */
        }
#ifdef __MMI_AVATAR__
        else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_AVATAR_VIDEO)
        {
            g_phb_cntx.selected_video_index = g_phb_cntx.total_video_id - g_phb_avatar_video_minus;
            g_phb_cntx.video_location = MMI_PHB_AVATAR_SELECT_PATH;
            g_phb_cntx.video_audio = 1; /* Turn on video's audio by default */
        }
#endif /* __MMI_AVATAR__ */ 
#ifdef __MMI_SWFLASH__
        else if (g_phb_cntx.recv_res_from == MMI_PHB_RECV_RES_SWFLASH)
        {
            g_phb_cntx.selected_video_index = g_phb_cntx.total_video_id - g_phb_swflash_video_minus;
            g_phb_cntx.video_location = MMI_PHB_SWFLASH_SELECT_PATH;
            g_phb_cntx.video_audio = 1; /* Turn on video's audio by default */
        }
#endif /* __MMI_SWFLASH__ */

        callerGroups[g_phb_cntx.active_index].VideoID = mmi_phb_video_update_by_index_for_path(callerGroups[g_phb_cntx.active_index].VideoID, &result);
        WriteRecord(NVRAM_EF_PHB_CALLER_GROUPS_LID, 1, callerGroups, CALLER_GROUPS_RECORD_SIZE, &pError);

        if (!result)    /* Update video fail. */
        {
            mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_OP_VIDEO_FAIL), MMI_EVENT_FAILURE);
        }
        else
        {
            mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS);
        }
        g_phb_cntx.selected_video_index = 0;
        g_phb_cntx.video_location = MMI_PHB_IMAGE_NO_SELECT;
    }
#endif /* __MMI_INCOMING_CALL_VIDEO__ */
    else
    {
        /* error handler */
        mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE);
    }
	/* delete screen history */
	DeleteScreenIfPresent(SCR_CALLER_GROUPS);
}

/*------------------------------------------------------  END Interface for File Manager Image------------------------------------------------*/
#endif /* defined(__MMI_FILE_MANAGER__) */ 


#define MMI_PHB_INTERFACE_FOR_EMAIL
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_email_save_entry_to_phb
 * DESCRIPTION
 *  save name and email to phonebook as a new entry
 * PARAMETERS
 *  ucs2_name       [IN]         Input email string pointer
 *  ucs2_email      [IN]         Input name string pointer
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_email_save_entry_to_phb(S8 *ucs2_name, S8 *ucs2_email)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_EMAIL_SAVE_ENTRY_TO_PHB);

    if (!mmi_phb_check_ready(MMI_TRUE))
    {
        return;
    }
    
    if (g_phb_cntx.phone_total == g_phb_cntx.phone_used)
    {
        mmi_display_popup((UI_string_type)GetString(STR_PHONE_FULL_MSG), MMI_EVENT_FAILURE);
    }
    else
    {
        g_phb_cntx.selected_storage = MMI_NVRAM;

        /* Clear other field */
        mmi_phb_util_clear_buffer(TRUE);

        /* Bring in data and enter save entry screen. */
        g_phb_cntx.set_done_flag = 1;
        if (ucs2_name)
        {
            mmi_ucs2ncpy(pbName, ucs2_name, MAX_PB_NAME_LENGTH);        	
            mmi_phb_truncate_pbname_buffer(MMI_NVRAM);
        }
    #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)            
        if (mmi_ucs2strlen(ucs2_email) < MAX_PB_EMAIL_LENGTH)
        {
            mmi_ucs2ncpy(pbEmailAddress, ucs2_email, mmi_ucs2strlen(ucs2_email));
        }
        else
        {
            mmi_ucs2ncpy(pbEmailAddress, ucs2_email, MAX_PB_EMAIL_LENGTH);            	
        }
    #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */            
        mmi_phb_clear_old_history();
    #ifdef __MMI_PHB_SIM_NUMBER_MANAGEMENT__
        g_phb_add_more_op = MMI_PHB_OP_NONE;
    #endif
        mmi_phb_entry_op_add_entry();
    }
}


#if defined(MMS_SUPPORT)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_dummy_screen
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_dummy_screen(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/    
    EntryNewScreen(GLOBAL_SCR_DUMMY, NULL, NULL, NULL);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_email_save_entry_to_phb_from_jmms
 * DESCRIPTION
 *  save the email entry from JMMS
 * PARAMETERS
 *  ucs2_email      [IN]         Input email string pointer
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_email_save_entry_to_phb_from_jmms(S8 *ucs2_email)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_EMAIL_SAVE_ENTRY_TO_PHB_FROM_JMMS);

    if (!mmi_phb_check_ready(MMI_TRUE))
    {
        return;
    }
    
    if (g_phb_cntx.phone_total == g_phb_cntx.phone_used)
    {
        mmi_display_popup((UI_string_type)GetString(STR_PHONE_FULL_MSG), MMI_EVENT_FAILURE);
    }
    else
    {
        /* Ensure the length will not exceed phonebook buffer. */
        if(mmi_ucs2strlen((S8*)ucs2_email) > MAX_PB_EMAIL_LENGTH)
        {
            memset((ucs2_email + MAX_PB_EMAIL_LENGTH * ENCODING_LENGTH), 0, 2);
        }

        /* Clear other field */
        mmi_phb_util_clear_buffer(TRUE);

        /* Bring in data and enter save entry screen. */
        g_phb_cntx.set_done_flag = 1;
    #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)            
        mmi_ucs2cpy(pbEmailAddress, ucs2_email);
    #endif            

        mmi_phb_clear_old_history();

        /* adding here */
        g_phb_cntx.selected_storage = MMI_NVRAM;

        if (g_phb_cntx.phone_used >= g_phb_cntx.phone_total)
        {
            mmi_display_popup((UI_string_type)GetString(STR_PBOOK_FULL_MSG), MMI_EVENT_FAILURE);
        }
        else
        {
        #ifdef __MMI_PHB_SIM_NUMBER_MANAGEMENT__
            g_phb_add_more_op = MMI_PHB_OP_NONE;
        #endif
            mmi_phb_entry_op_add_entry();
        }
    }
}
#endif /* defined(MMS_SUPPORT) */ 


#define MMI_PHB_INTERFACE_FOR_VRSD_DIAL
#if defined(__MMI_VRSD_DIAL__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_vrsddial_enter_tag_list
 * DESCRIPTION
 *  Phonebook list interface for voice dial application.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_vrsddial_enter_tag_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i, j;
    U16 EntryCount = 0;
    U16 store_index;
    vrsd_tag_struct tag_list[MMI_VRSD_MAX_TAG];
    S16 pError;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!mmi_phb_check_ready(MMI_TRUE))
    {
        return;
    }
    /* Allocate buffer for phonebook memeber list filter */
    mmi_phb_list_allocate_filter();

    /* Read voice tag list from NVRAM */
    ReadRecord(
        NVRAM_EF_VRSD_TAG_LID,
        MMI_VRSD_GROUP_ALL,
        (void*)tag_list,
        MMI_VRSD_MAX_TAG * sizeof(vrsd_tag_struct),
        &pError);

    /* Find out entry with tag and put it into filter list */
    for (i = 0; i < PhoneBookEntryCount; i++)
    {
        store_index = g_phb_name_index[i];

        for (j = 0; j < MMI_VRSD_MAX_TAG; j++)
        {
            if ((tag_list[j].appref_id < 0xffff) && (tag_list[j].app_id == MMI_VRSD_APP_DIAL) &&
                (tag_list[j].appref_id == store_index))
            {
                g_phb_cntx.list_filter[EntryCount] = store_index;
                EntryCount++;
                break;
            }
        }
    }

    /* If voice tag exists, enter phonebook list */
    if (EntryCount)
    {
        g_phb_cntx.highlight_entry = 0;
        phbListView = MMI_PHB_LIST_FOR_VR;
        mmi_phb_entry_list(
            EntryCount,                             /* Total Entry */
            STR_ID_VRSD_DIAL,                       /* Title String */
            MAIN_MENU_TITLE_MULTIMEDIA_ICON,        /* Title Image */
            STR_GLOBAL_OPTIONS,                     /* LSK */
            IMG_GLOBAL_OPTIONS,                     /* LSK */
            mmi_phb_vrsddial_get_tag_list_index,    /* Highlight Callback */
            mmi_vrsddial_main_menu_all_option,      /* LSK Callback */
            NULL,                                   /* SEND Key Callback */
            mmi_phb_filter_list_get_item,           /* List Callback */
            mmi_phb_filter_list_get_hint,           /* Hint Callback */
            mmi_phb_vrsddial_enter_tag_list,        /* Re-Entry Callback */
            FALSE,                                  /* Alpha Index */
            TRUE);                                  /* Right Arrow Key */

    }
    else    /* No voice Tag exists, Go to menu directly. */
    {
        mmi_vrsddial_entry_empty_list();
        mmi_phb_list_free_filter();
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_vrsddial_get_tag_list_index
 * DESCRIPTION
 *  This function is just for template.
 * PARAMETERS
 *  nIndex      [IN]            
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_vrsddial_get_tag_list_index(S32 nIndex)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.active_index = g_phb_cntx.list_filter[nIndex];   /* This is store_index in filter list */

    StopTimer(VRSD_DIAL_PLAYBACK_TIMER);
    StartTimer(VRSD_DIAL_PLAYBACK_TIMER, MMI_VRSD_PLAY_TAG_DELAY, mmi_phb_vrsddial_playback_by_store_index);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_vrsddial_playback_by_store_index
 * DESCRIPTION
 *  This function is just for template.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_vrsddial_playback_by_store_index(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 tag_id;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    tag_id = mmi_vrsd_util_get_tag_id(MMI_VRSD_APP_DIAL, g_phb_cntx.active_index);
    mmi_vrsd_playback_req(tag_id, NULL);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_vrsddial_enter_no_tag_list
 * DESCRIPTION
 *  Phonebook list interface for voice dial application.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_vrsddial_enter_no_tag_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i, j;
    U16 EntryCount = 0;
    U16 store_index;
    vrsd_tag_struct tag_list[MMI_VRSD_MAX_TAG];
    S16 pError;
    BOOL has_tag;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!mmi_phb_check_ready(MMI_TRUE))
    {
        return;
    }
    /* Allocate buffer for phonebook memeber list filter */
    mmi_phb_list_allocate_filter();

    /* Read voice tag list from NVRAM */
    ReadRecord(
        NVRAM_EF_VRSD_TAG_LID,
        MMI_VRSD_GROUP_ALL,
        (void*)tag_list,
        MMI_VRSD_MAX_TAG * sizeof(vrsd_tag_struct),
        &pError);

    /* Find out entry without voice tag and put it into filter list */
    for (i = 0; i < PhoneBookEntryCount; i++)
    {
        store_index = g_phb_name_index[i];

        has_tag = FALSE;
        for (j = 0; j < MMI_VRSD_MAX_TAG; j++)
        {
            if ((tag_list[j].app_id == MMI_VRSD_APP_DIAL) && (tag_list[j].appref_id == store_index))
            {
                has_tag = TRUE;
                break;
            }
        }

        /* Add to list if the entry has no tag. */
        if (!has_tag)
        {
            g_phb_cntx.list_filter[EntryCount] = store_index;
            EntryCount++;
        }
    }

    /* If voice tag exists, enter phonebook list */
    if (EntryCount)
    {
        g_phb_cntx.highlight_entry = 0;
        phbListView = MMI_PHB_LIST_FOR_VR;
        mmi_phb_entry_list(
            EntryCount,                                 /* Total Entry */
            STR_ID_VRSD_DIAL,                           /* Title String */
            MAIN_MENU_TITLE_MULTIMEDIA_ICON,            /* Title Image */
            STR_GLOBAL_OK,                              /* LSK */
            IMG_GLOBAL_OK,                              /* LSK */
            mmi_phb_get_index_by_store_location_second, /* Highlight Callback */
            mmi_vrsddial_add_tag_from_list,             /* LSK Callback */
            NULL,                                       /* SEND Key Callback */
            mmi_phb_filter_list_get_item,               /* List Callback */
            mmi_phb_filter_list_get_hint,               /* Hint Callback */
            mmi_phb_vrsddial_enter_no_tag_list,         /* Re-Entry Callback */
            FALSE,                                      /* Alpha Index */
            TRUE);                                      /* Right Arrow Key */

    }
    else    /* All Entries has voice tag. */
    {
        mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_NO_ENTRY_TO_SELECT), MMI_EVENT_FAILURE);
        mmi_phb_list_free_filter();

    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_highlight_op_voice_dial
 * DESCRIPTION
 *  This function is just for template.
 * PARAMETERS
 *  void
 * RETURNS
 *  viod
 *****************************************************************************/
void mmi_phb_highlight_op_voice_dial(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SetLeftSoftkeyFunction(mmi_vrsddial_phb_main_menu_pre_entry, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

    SetKeyHandler(mmi_vrsddial_phb_main_menu_pre_entry, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_vrsddial_auto_dial
 * DESCRIPTION
 *  This function is just for template.
 * PARAMETERS
 *  store_index     [IN]            
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_vrsddial_auto_dial(U16 store_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.active_index = 0;

    for (i = 0; i < PhoneBookEntryCount; i++)
    {
        if (g_phb_name_index[i] == store_index)
        {
            g_phb_cntx.active_index = i;
            break;
        }
    }

    mmi_phb_dial_normal_call();
}

#endif /* defined(__MMI_VRSD_DIAL__) */ 


#define MMI_PHB_INTERFACE_FOR_VRSI
#if defined(__MMI_VRSI__) && defined(__MMI_VRSI_TRAIN_TAG__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_vrsi_list_get_item
 * DESCRIPTION
 *  This function is just for template.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
pBOOL mmi_phb_vrsi_list_get_item(S32 item_index, UI_string_type str_buff, PU8 *img_buff_p, U8 str_img_mask)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if ((item_index < 0) || (item_index >= PhoneBookEntryCount))
    {
        return FALSE;
    }

    store_index = g_phb_name_index[item_index];

    if (mmi_ucs2strlen((S8*) PhoneBook[store_index].alpha_id.name))
    {
        mmi_ucs2ncpy((S8*) str_buff, (S8*) PhoneBook[store_index].alpha_id.name, MAX_SUBMENU_CHARACTERS);
    }
    else
    {
        mmi_ucs2ncpy((S8*) str_buff, GetString(STR_ID_PHB_UNNAMED), MAX_SUBMENU_CHARACTERS);
    }

    /* Check if phonebook item has VRSD tag */
    if (mmi_vrsi_train_util_get_loc_in_list(MMI_VRSI_APP_DIAL, store_index, g_phb_vrsi_tag_list) != 0xff)
    {
        *img_buff_p = get_image(IMG_ID_VRSI_APP);
    }
    else
    {
    #ifdef __MMI_DUAL_SIM_MASTER__
    	if(store_index >= (MAX_PB_PHONE_ENTRIES+MAX_PB_SIM_ENTRIES))
    	{
            *img_buff_p = get_image(IMG_ID_CARD2_PHB_STORAGE_SIM);
    	}
    	else if (store_index >= MAX_PB_PHONE_ENTRIES)
    	{
            *img_buff_p = get_image(IMG_ID_CARD1_PHB_STORAGE_SIM);
    	}
    	else
    	{
            *img_buff_p = get_image(IMG_STORAGE_HANDSET);
    	}
    #else /* __MMI_DUAL_SIM_MASTER__ */
        if (store_index >= MAX_PB_PHONE_ENTRIES)
        {
            *img_buff_p = get_image(IMG_STORAGE_SIM);
        }
        else
        {
            *img_buff_p = get_image(IMG_STORAGE_HANDSET);
        }
    #endif /* __MMI_DUAL_SIM_MASTER__ */
    }

    return TRUE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_vrsi_pre_entry_list
 * DESCRIPTION
 *  This function is just for template.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_vrsi_pre_entry_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S16 pError;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.highlight_entry = 0;
    phbListView = MMI_PHB_LIST_FOR_VR;
    g_phb_cntx.list_filter = NULL;

    /* Allocate memory for list to temporarily store vrsi tag list */
    g_phb_vrsi_tag_list = OslMalloc(MMI_VRSI_MAX_SD_TAG * sizeof(vrsi_sd_tag_struct));
    ReadRecord(NVRAM_EF_VRSI_TAG_LID, 1, (void*)g_phb_vrsi_tag_list, MMI_VRSI_MAX_SD_TAG * sizeof(vrsi_sd_tag_struct), &pError);

    mmi_phb_entry_list(
        PhoneBookEntryCount,                /* Total Entry */
        STR_ID_VRSI_NAME_LIST,              /* Title String */
        MAIN_MENU_TITLE_MULTIMEDIA_ICON,    /* Title Image */
        STR_GLOBAL_OPTIONS,                 /* LSK */
        IMG_GLOBAL_OPTIONS,                 /* LSK */
        mmi_phb_get_index_second_level,     /* Highlight Callback */
        mmi_phb_vrsi_entry_list_callback,   /* LSK Callback */
        NULL,                               /* SEND Key Callback */
        mmi_phb_vrsi_list_get_item,         /* List Callback */
        mmi_phb_list_get_hint,              /* Hint Callback */
        mmi_phb_vrsi_pre_entry_list,        /* Re-Entry Callback */
        TRUE,                               /* Alpha Index */
        TRUE);                              /* Right Arrow Key */
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_vrsi_exit_entry_list
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_vrsi_exit_entry_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    if(g_phb_vrsi_tag_list)
    {
        OslMfree(g_phb_vrsi_tag_list);
        g_phb_vrsi_tag_list = NULL;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_vrsi_entry_list_callback
 * DESCRIPTION
 *  This function is just for template.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_vrsi_entry_list_callback(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    store_index = g_phb_name_index[g_phb_cntx.active_index_second];
    mmi_vrsi_ndial_train_list_callback(store_index);
}
#endif /* defined(__MMI_VRSI__) && defined(__MMI_VRSI_TRAIN_TAG__) */ 


#ifdef __SYNCML_SUPPORT__
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_get_phone_index_arrary
 * DESCRIPTION
 *  
 * PARAMETERS
 *  index_array       [OUT]        
 * RETURNS
 *  U16
 *****************************************************************************/
U16 mmi_phb_get_phone_index_arrary(U16* index_array)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;
    U16 array_count = 0;
    U16 store_index;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_DUAL_SIM_MASTER__
     if (MTPNP_PFAL_Phb_IsReady() == MTPNP_FALSE)
#else /* __MMI_DUAL_SIM_MASTER__ */
    if (!g_phb_cntx.phb_ready || g_phb_cntx.processing)
#endif /* __MMI_DUAL_SIM_MASTER__ */
    {
        return 0xFFFF;
    }

    if (index_array != NULL)
    {
        for (i = 0; i < PhoneBookEntryCount; i++)
        {
            store_index = g_phb_name_index[i];
            if (store_index < MAX_PB_PHONE_ENTRIES)
            {
                index_array[array_count] = store_index;
                array_count++;
            }
        }
    }
    return g_phb_cntx.phone_used;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_sync_vcard
 * DESCRIPTION
 *  
 * PARAMETERS
 *  path        [IN]         
 *  index       [IN]        
 * RETURNS
 *  
 *****************************************************************************/
void mmi_phb_sync_vcard(U8 action, S8 *path, U16 index, U8 sync_charset)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    FS_HANDLE fh;
    U8 error_code = MMI_VCARD_PARSE_NO_ERROR;
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) || defined(__MMI_PHB_INFO_FIELD__)
    S16 pError;
#endif
    S8 number_buff[MAX_PB_NUMBER_LENGTH + 1 + 1];
    S8 number_buff2[MAX_PB_NUMBER_LENGTH + 1 + 1];
#if defined(__MMI_PHB_BIRTHDAY_FIELD__)
    S8 b_day[9];    /* Format: yyyymmdd */
#endif
    S32 get_result;
    PHB_OPTIONAL_IDS_STRUCT *opt_ids = NULL;    
    vcard_field_struct field_content;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    if (action != MMI_PHB_VCARD_DELETE && path == NULL)
    {
        mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_ERROR, index);
        return;
    }    
    if ((action != MMI_PHB_VCARD_ADD) && index >= MAX_PB_PHONE_ENTRIES)
    {
        mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_OUT_OF_INDEX, index);        
        return;
    }
#ifdef __MMI_DUAL_SIM_MASTER__
    if (MTPNP_PFAL_Phb_IsReady() == MTPNP_FALSE)
#else /* __MMI_DUAL_SIM_MASTER__ */
    if (!g_phb_cntx.phb_ready || g_phb_cntx.processing)
#endif /* __MMI_DUAL_SIM_MASTER__ */
    {
        mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_NOT_READY, index);
        return;
    }
#ifdef __MMI_PHB_USIM_FIELD__
    if (g_phb_cntx.is_usim && g_phb_cntx.usim_ready_stage != MMI_PHB_USIM_READY)
    {
        mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_NOT_READY, index);
        return;
    }
#endif /* __MMI_PHB_USIM_FIELD__ */    
    if (action == MMI_PHB_VCARD_READ)
    {
        memset((void*)&field_content, 0, sizeof(field_content));
        /* Mobile Number */
        if (((PhoneBook[index].tel.type & 0x90) == 0x90) && (PhoneBook[index].tel.type != 0xFF))
        {
            mmi_phb_convert_to_digit_by_store_index((U8*) number_buff2, PhoneBook[index].tel.number, MAX_PB_NUMBER_LENGTH + 1, index);
            /* now number is +**21*8123456# */
            /* call the third API to transfer + to place just after SS pattern */
            applib_decode_number((U8*)number_buff2, (U8*)number_buff, 0x91);
            /* now number is **21*+8123456#, so we can show the original data to user */
        }
        else
        {
            mmi_phb_convert_to_digit_by_store_index((U8*) number_buff, PhoneBook[index].tel.number, MAX_PB_NUMBER_LENGTH + 1, index);
        }

        field_content.ucs2_name = (S8*) PhoneBook[index].alpha_id.name,
        field_content.number = (S8*) number_buff;

    #if defined(__MMI_PHB_BIRTHDAY_FIELD__)
        mmi_vcard_get_bday_to_buff(index, (S8*) b_day);
        field_content.b_day = b_day;
    #endif /* defined(__MMI_PHB_BIRTHDAY_FIELD__) */ 
    #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
        ReadRecord(
            NVRAM_EF_PHB_FIELDS_LID,
            (U16) (index + 1),
            (void*)&PhoneBookOptionalFields,
            OPTIONAL_FIELDS_RECORD_SIZE,
            &pError);
        field_content.home_num = (S8*) PhoneBookOptionalFields.homeNumber;
        field_content.office_num = (S8*) PhoneBookOptionalFields.officeNumber;
        field_content.fax_num = (S8*) PhoneBookOptionalFields.faxNumber;
        field_content.email_addr = (S8*) PhoneBookOptionalFields.emailAddress;
    #ifdef __MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__
        field_content.email_addr2 = (S8*) PhoneBookOptionalFields.emailAddress2;
    #endif
        field_content.ucs2_company = (S8*) PhoneBookOptionalFields.companyName;
    #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
    #if defined(__MMI_PHB_INFO_FIELD__)
        ReadRecord(
            NVRAM_EF_PHB_INFO_LID,
            (U16) (index + 1),
            (void*)&PhoneBookInfoFields,
            MMI_PHB_INFO_RECORD_SIZE,
            &pError);
        field_content.info_fields = &PhoneBookInfoFields;
    #endif /* defined(__MMI_PHB_INFO_FIELD__) */
        mmi_phb_read_optional_ids(index);
        opt_ids = (PHB_OPTIONAL_IDS_STRUCT*) g_phb_cntx.optional_ids;
        field_content.optional_ids = opt_ids;

    #if defined(__MMI_PHB_GEOGRAPHIC_FIELD__)
        field_content.geo_fields = &PhoneBookGeoFields,
    #endif

        get_result = mmi_vcard_writer_v21_to_file(path, &field_content);

        if (get_result != VOBJ_ERR_NO_ERROR)
        {
            mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_ERROR, index);
        }
        else
        {
            mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_SUCCESS, index);
        }
        return;
    }
    else if (action == MMI_PHB_VCARD_ADD || action == MMI_PHB_VCARD_UPDATE)
    {
        if (action == MMI_PHB_VCARD_ADD)
        {
            if (g_phb_cntx.phone_used >= g_phb_cntx.phone_total) /* NVRAM Only */
            {
                mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_STORAGE_FULL, index);
                return;
            }
            g_phb_cntx.selected_storage = MMI_NVRAM;
        }

        fh = FS_Open((U16*) path, FS_READ_ONLY);
        if (fh <= 0)
        {
            mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_OPEN_FILE_ERROR, index);
            return;
        }
        mmi_phb_util_clear_buffer(TRUE);  /* Clear all phonebook vcard temp buffer before parsing */
        mmi_vcard_util_set_default_charset(sync_charset);
        error_code = mmi_vcard_reader_parse_line(fh);
        FS_Close(fh);
        mmi_phb_entry_op_remove_invalid_name_and_number();
    }
    
    if (action == MMI_PHB_VCARD_DELETE)
    {
        if (mmi_phb_util_check_entry_exist(index))
        {
            g_phb_enter_from = MMI_PHB_ENTER_FROM_SYNCML;
            g_phb_cntx.active_index = index;
            mmi_phb_op_delete_entry_req();
        }
        else /* Entry already deleted by handset user (Client) */
        {
            mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_NOT_FOUND, index);
        }
        return;
    }
    
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_SYNC_VCARD_ERROR_CODE, error_code);
	
    g_phb_enter_from = MMI_PHB_ENTER_FROM_SYNCML;

    /*  Check if parse and validation ok. */
    if (error_code != MMI_VCARD_PARSE_NO_ERROR || !mmi_phb_op_check_pre_save(FALSE))
    {
        g_phb_enter_from = MMI_PHB_ENTER_NONE;

        mmi_syncml_phb_sync_rsp(MMI_PHB_SYNC_ERROR, index);
        return;
    }

    if (action == MMI_PHB_VCARD_ADD)
    {
        mmi_phb_op_add_entry_req();
    }
    else if (action == MMI_PHB_VCARD_UPDATE)
    {
        g_phb_cntx.active_index = index;
        mmi_phb_op_edit_entry_req();
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_sync_get_record_size
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  U16 record_size
 *
 *****************************************************************************/
U16 mmi_phb_sync_get_record_size(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 record_size = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    record_size += ((MAX_PB_NAME_LENGTH + 1) + (MAX_PB_NUMBER_LENGTH + 1 + 1));
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
    record_size += NVRAM_PHB_FIELDS_SIZE;
#endif
#if defined(__MMI_PHB_BIRTHDAY_FIELD__)
    record_size += (NVRAM_EF_PHB_BIRTHDAY_SIZE / NVRAM_EF_PHB_BIRTHDAY_COUNT);
#endif
#if defined(__MMI_PHB_INFO_FIELD__)
    record_size += NVRAM_EF_PHB_INFO_SIZE;
#endif
    return record_size;
}
#endif /* __SYNCML_SUPPORT__ */


#ifdef __MMI_BIRTHDAY_REMINDER__
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_get_name_number
 * DESCRIPTION
 *  check if an entry exists
 * PARAMETERS
 *  store_index     [IN]        The store location id of the entry
 *  out_name        [OUT]       The name of the phonebook entry (UCS2)
 *  out_number      [OUT]       The default mobile number of the entry (ASCII)
 * RETURNS
 *  TRUE if the location has the entry, else FALSE
 *****************************************************************************/
MMI_BOOL mmi_phb_get_name_number(U16 store_index, U8* out_name, U8* out_number)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 tempNumber[MAX_PB_NUMBER_LENGTH + 1 + 1];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __SYNCML_SUPPORT__
    if (mmi_syncml_is_phb_sync_now())
    {
        return MMI_FALSE;
    }
    else
#endif /* __SYNCML_SUPPORT__ */
#ifdef __MMI_DUAL_SIM_MASTER__
    if (MTPNP_PFAL_Phb_IsReady() == !MTPNP_TRUE)
#else/* __MMI_DUAL_SIM_MASTER__ */
    if (!g_phb_cntx.phb_ready || g_phb_cntx.processing)
#endif/* __MMI_DUAL_SIM_MASTER__ */
    {
        return MMI_FALSE;
    }

    mmi_ucs2cpy((S8*)out_name, (PS8) PhoneBook[store_index].alpha_id.name);
    if (((PhoneBook[store_index].tel.type & 0x90) == 0x90) && (PhoneBook[store_index].tel.type != 0xFF))
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, PhoneBook[store_index].tel.number, MAX_PB_NUMBER_LENGTH + 1, store_index);
        /* now number is +**21*8123456# */
        /* call the third API to transfer + to place just after SS pattern */
        applib_decode_number((U8*)tempNumber, (U8*)out_number, 0x91);
        /* now number is **21*+8123456#, so we can show the original data to user */
    }
    else
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) out_number, PhoneBook[store_index].tel.number, MAX_PB_NUMBER_LENGTH + 1, store_index);
    }
    return MMI_TRUE;
}
#endif /* __MMI_BIRTHDAY_REMINDER__ */


/* Convert function for phonebook numbers between BCD and ASCII format. */
#define MMI_PHB_GENERAL_UTIL
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_util_check_entry_exist
 * DESCRIPTION
 *  check if an entry exists
 * PARAMETERS
 *  store_index     [IN]        The store location id of the entry
 * RETURNS
 *  TRUE if the location has the entry, else FALSE
 *****************************************************************************/
BOOL mmi_phb_util_check_entry_exist(U16 store_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (i = 0; i < PhoneBookEntryCount; i++)
    {
        if (g_phb_name_index[i] == store_index)
        {
            return TRUE;
        }
    }
    return FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_util_check_additional_number_exist
 * DESCRIPTION
 *  check if any additional number exists
 * PARAMETERS
 *  store_index     [IN]        The store location id of the entry
 * RETURNS
 *  TRUE if the location has any additional number, else FALSE
 *****************************************************************************/
BOOL mmi_phb_util_check_additional_number_exist(U16 store_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return ((PhoneBook[store_index].field & MMI_PHB_ENTRY_FIELD_HOME) ||
            (PhoneBook[store_index].field & MMI_PHB_ENTRY_FIELD_OFFICE) ||
            (PhoneBook[store_index].field & MMI_PHB_ENTRY_FIELD_FAX));
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_to_bcd_get_byte
 * DESCRIPTION
 *  convert ASCII character to BCD
 * PARAMETERS
 *  digit       [IN]        Ascii character
 * RETURNS
 *  bcd character
 *****************************************************************************/
U8 mmi_phb_convert_to_bcd_get_byte(U8 digit)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 half_byte;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (digit <= '9' && digit >= '0')
    {
        half_byte = digit - '0';
    }
    else
    {
        switch (digit)
        {
            case '*':
                half_byte = 0x0a;
                break;

            case '#':
                half_byte = 0x0b;
                break;

            case 'p':
            case 'P':
                half_byte = 0x0c;
                break;

            case 'w':
            case 'W':
            case '?':
                half_byte = 0x0d;
                break;

            case '+':
                half_byte = 0x0e;
                break;

            default:
                half_byte = 0x0f;
                break;
        }
    }
    return half_byte;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_to_bcd
 * DESCRIPTION
 *  convert ASCII string to BCD. (If reaches max length, there will be no terminate '0x0f')
 * PARAMETERS
 *  dest                [IN/OUT]        An BCD encoding string.
 *  source              [IN]            An ASCII encoding string.
 *  max_dest_len        [IN]            The byte size of dest array.
 * RETURNS
 *  byte number after convert.
 *****************************************************************************/
U8 mmi_phb_convert_to_bcd(U8 *dest, U8 *source, U8 max_dest_len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 i = 0;
    U8 lower_byte, upper_byte;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (source[i] != '\0')
    {

        lower_byte = mmi_phb_convert_to_bcd_get_byte(source[i]);
        upper_byte = mmi_phb_convert_to_bcd_get_byte(source[i + 1]);

        if ((i >> 1) >= max_dest_len)
        {
            return (i >> 1);
        }

        *((U8*) dest + (i >> 1)) = (upper_byte << 4) + lower_byte;

        if (source[i + 1] == '\0')
        {
            if ((i >> 1) >= max_dest_len)
            {
                return (i >> 1) + 1;
            }

            *((U8*) dest + (i >> 1)) |= 0xf0;
            return (i >> 1) + 1;
        }

        i += 2;
    }

    if ((i >> 1) >= max_dest_len)
    {
        return (i >> 1);
    }

    *((U8*) dest + (i >> 1)) = 0xff;
    return (i >> 1);

}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_to_digit_get_byte_by_storage
 * DESCRIPTION
 *  convert BCD character to ASCII
 * PARAMETERS
 *  bcd     [IN]        Bcd character
 * RETURNS
 *  ascii character
 *****************************************************************************/
U8 mmi_phb_convert_to_digit_get_byte_by_storage(U8 bcd, U8 storage)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 digit;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/    
    if (!(storage == MMI_NVRAM || storage == MMI_SIM
    #ifdef __MMI_DUAL_SIM_MASTER__
        || storage == MMI_SIM2
    #endif
        ))
    {
        ASSERT(0);
    }

    if (bcd <= 9)
    {
        digit = bcd + '0';
    }
    else
    {
        switch (bcd)
        {
            case 0x0a:
                digit = '*';
                break;

            case 0x0b:
                digit = '#';
                break;

            case 0x0c:
                digit = 'p';
                break;

            case 0x0d:
                if (storage == MMI_NVRAM)
                {
                    digit = 'w';
                }
                else
                {
                    digit = '?';
                }
                break;

            case 0x0e:
                digit = '+';
                break;

            default:
                digit = '\0';
                break;
        }
    }
    return digit;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_to_digit
 * DESCRIPTION
 *  convert BCD string to ASCII
 * PARAMETERS
 *  dest                [IN/OUT]        An ASCII encoding string.
 *  source              [IN]            An BCD encoding string.
 *  max_dest_len        [IN]            The byte size of dest array.(include null terminate '\0')
 * RETURNS
 *  byte number after convert.
 *****************************************************************************/
U8 mmi_phb_convert_to_digit_by_storage(U8 *dest, U8 *source, U8 max_dest_len, U8 storage)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 ch1, ch2;
    U8 i = 0, j = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!(storage == MMI_NVRAM || storage == MMI_SIM
    #ifdef __MMI_DUAL_SIM_MASTER__
        || storage == MMI_SIM2
    #endif
        ))
    {
        ASSERT(0);
    }
    
    while ((source[i] != 0xff) && (j < max_dest_len - 1))
    {
        ch1 = source[i] & 0x0f;
        ch2 = (source[i] & 0xf0) >> 4;

        *((U8*) dest + j) = mmi_phb_convert_to_digit_get_byte_by_storage(ch1, storage);

        if (ch2 == 0x0f)
        {
            *((U8*) dest + j + 1) = '\0';
            return j + 1;
        }
        else
        {
            *((U8*) dest + j + 1) = mmi_phb_convert_to_digit_get_byte_by_storage(ch2, storage);
        }
        i++;
        j += 2;
    }

    *((U8*) dest + j) = '\0';
    return j;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_to_digit
 * DESCRIPTION
 *  convert BCD string to ASCII
 * PARAMETERS
 *  dest                [IN/OUT]        An ASCII encoding string.
 *  source              [IN]            An BCD encoding string.
 *  max_dest_len        [IN]            The byte size of dest array.(include null terminate '\0')
 * RETURNS
 *  byte number after convert.
 *****************************************************************************/
U8 mmi_phb_convert_to_digit_by_store_index(U8 *dest, U8 *source, U8 max_dest_len, U16 store_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 storage;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (store_index >= MAX_PB_ENTRIES)
        ASSERT(0);

    if (store_index < MAX_PB_PHONE_ENTRIES)
    {
        storage = MMI_NVRAM;
    }
#ifdef __MMI_DUAL_SIM_MASTER__
    else if (store_index >= (MAX_PB_PHONE_ENTRIES + MAX_PB_SIM_ENTRIES))
    {
        storage =  MMI_SIM2;
    }
#endif
    else
    {
        storage =  MMI_SIM;
    }
    
    return mmi_phb_convert_to_digit_by_storage(dest, source, max_dest_len, storage);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_get_ucs2_number
 * DESCRIPTION
 *  This function returns the UCS2 number string by specifying store_index
 * PARAMETERS
 *  pString     [IN/OUT]        Buffter to store convert result
 *  index       [IN]            Store index of the phonebook entry to be converted.
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_convert_get_ucs2_number(S8 *pString, U16 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 tempNumber[MAX_PB_NUMBER_LENGTH + 1 + 1];
    U8 tempNumber2[MAX_PB_NUMBER_LENGTH + 1 + 1];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (((PhoneBook[index].tel.type & 0x90) == 0x90) && (PhoneBook[index].tel.type != 0xFF))
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber2, PhoneBook[index].tel.number, MAX_PB_NUMBER_LENGTH + 1, index);
        /* now number is +**21*8123456# */
        /* call the third API to transfer + to place just after SS pattern */
        applib_decode_number(tempNumber2, tempNumber, 0x91);
        /* now number is **21*+8123456#, so we can show the original data to user */
    }
    else
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, PhoneBook[index].tel.number, MAX_PB_NUMBER_LENGTH + 1, index);
    }

    mmi_asc_to_ucs2(pString, (S8*) tempNumber);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_is_char_in_bcd
 * DESCRIPTION
 *  convert BCD string to ASCII
 * PARAMETERS
 *  dest                [IN/OUT]        An ASCII encoding string.
 *  source              [IN]            An BCD encoding string.
 *  max_dest_len        [IN]            The byte size of dest array.(include null terminate '\0')
 * RETURNS
 *  byte number after convert.
 *****************************************************************************/
MMI_BOOL mmi_phb_is_num_in_bcd(const U8* source, U8 num, U16 len_in_bytes)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 ch1, ch2;
    U8 i = 0;
    MMI_BOOL result = MMI_FALSE;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (source[i] != 0xff && i < len_in_bytes)
    {
        ch1 = source[i] & 0x0f;
        ch2 = (source[i++] & 0xf0) >> 4;

        if (num == ch1 || num == ch2)
        {
            result = MMI_TRUE;
            break;
        }
    }
    return result;
}


#ifdef __MMI_PHB_USIM_FIELD__
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_get_ucs2_anr
 * DESCRIPTION
 *  This function returns the UCS2 number string by specifying store_index
 * PARAMETERS
 *  anra            [?]             
 *  anrb            [?]             
 *  anrc            [?]             
 *  index           [IN]            Store index of the phonebook entry to be converted.
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_convert_get_ucs2_anr(S8 *anra, S8 *anrb, S8 *anrc, U16 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 tempNumber[MAX_PB_NUMBER_LENGTH + 1 + 1];
    U8 tempNumber2[MAX_PB_NUMBER_LENGTH + 1 + 1];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(index >= MAX_PB_PHONE_ENTRIES);
    index -= MAX_PB_PHONE_ENTRIES;

    if (((phb_anr[index][0].type & 0x90) == 0x90) && (phb_anr[index][0].type != 0xFF))
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber2, phb_anr[index][0].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
        /* now number is +**21*8123456# */
        /* call the third API to transfer + to place just after SS pattern */
        applib_decode_number(tempNumber2, tempNumber, 0x91);
        /* now number is **21*+8123456#, so we can show the original data to user */
    }
    else
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, phb_anr[index][0].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
    }
    mmi_asc_to_ucs2(anra, (S8*) tempNumber);
    if (((phb_anr[index][1].type & 0x90) == 0x90) && (phb_anr[index][1].type != 0xFF))
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber2, phb_anr[index][1].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
        /* now number is +**21*8123456# */
        /* call the third API to transfer + to place just after SS pattern */
        applib_decode_number(tempNumber2, tempNumber, 0x91);
        /* now number is **21*+8123456#, so we can show the original data to user */
    }
    else
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, phb_anr[index][1].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
    }
    mmi_asc_to_ucs2(anrb, (S8*) tempNumber);
    if (((phb_anr[index][2].type & 0x90) == 0x90) && (phb_anr[index][2].type != 0xFF))
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber2, phb_anr[index][2].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
        /* now number is +**21*8123456# */
        /* call the third API to transfer + to place just after SS pattern */
        applib_decode_number(tempNumber2, tempNumber, 0x91);
        /* now number is **21*+8123456#, so we can show the original data to user */
    }
    else
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, phb_anr[index][2].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
    }
    mmi_asc_to_ucs2(anrb, (S8*) tempNumber);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_get_ansii_anr
 * DESCRIPTION
 *  This function returns the ansii number string by specifying store_index
 * PARAMETERS
 *  anra            [?]             
 *  anrb            [?]             
 *  anrc            [?]             
 *  index           [IN]            Store index of the phonebook entry to be converted.
 *  pString(?)      [IN/OUT]        Buffter to store convert result
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_convert_get_ansii_anr(S8 *anra, S8 *anrb, S8 *anrc, U16 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S8 tempNumber[MAX_PB_NUMBER_LENGTH + 1 + 1];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(index >= MAX_PB_PHONE_ENTRIES);
    index -= MAX_PB_PHONE_ENTRIES;

    if (((phb_anr[index][0].type & 0x90) == 0x90) && (phb_anr[index][0].type != 0xFF))
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, phb_anr[index][0].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
        /* now number is +**21*8123456# */
        /* call the third API to transfer + to place just after SS pattern */
        applib_decode_number((U8*)tempNumber, (U8*)anra, 0x91);
        /* now number is **21*+8123456#, so we can show the original data to user */
    }
    else
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) anra, phb_anr[index][0].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
    }

    if (((phb_anr[index][1].type & 0x90) == 0x90) && (phb_anr[index][1].type != 0xFF))
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, phb_anr[index][1].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
        /* now number is +**21*8123456# */
        /* call the third API to transfer + to place just after SS pattern */
        applib_decode_number((U8*)tempNumber, (U8*)anrb, 0x91);
        /* now number is **21*+8123456#, so we can show the original data to user */
    }
    else
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) anrb, phb_anr[index][1].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
    }

    if (((phb_anr[index][2].type & 0x90) == 0x90) && (phb_anr[index][2].type != 0xFF))
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) tempNumber, phb_anr[index][2].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
        /* now number is +**21*8123456# */
        /* call the third API to transfer + to place just after SS pattern */
        applib_decode_number((U8*)tempNumber, (U8*)anrc, 0x91);
        /* now number is **21*+8123456#, so we can show the original data to user */
    }
    else
    {
        mmi_phb_convert_to_digit_by_store_index((U8*) anrc, phb_anr[index][2].number, MAX_PB_NUMBER_LENGTH + 1, (index + MAX_PB_PHONE_ENTRIES));
    }
}

#endif /* __MMI_PHB_USIM_FIELD__ */ 


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_to_0x81_get_offset
 * DESCRIPTION
 *  This function get the offset according to the base for a two bytes UCS2 character.
 * PARAMETERS
 *  base        [IN]        Base of the character.
 *  code        [IN]        Character for getting offset
 * RETURNS
 *  offset
 *****************************************************************************/
U8 mmi_phb_convert_to_0x81_get_offset(U16 base, U16 code)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    return (U8) (code - base + 0x80);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_to_0x81_get_base
 * DESCRIPTION
 *  This function get the base for a two bytes UCS2 chatacters.(For Thai only now.)
 * PARAMETERS
 *  code        [IN]        Input character
 * RETURNS
 *  the offset base
 *****************************************************************************/
U16 mmi_phb_convert_to_0x81_get_base(U16 code)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if ((code >= 0x0E00) && (code <= 0x0E5F))   /* Thai 0E00 ~ 0E5F */
    {
        return 0x0E00;
    }
    if ((code >= 0x0080) && (code <= 0x00FF))   /* C1 Controls and Latin-1 Supplement */
    {
        return 0x0080;
    }
    if ((code >= 0x0100) && (code <= 0x017F))   /* Latin Extended - A */
    {
        return 0x0100;
    }
    if ((code >= 0x0180) && (code <= 0x01FF))   /* Latin Extended - B (Partial) */
    {
        return 0x0180;
    }
    if ((code >= 0x0400) && (code <= 0x047F))   /* Russian 0400 ~ 04FF */
    {
        return 0x0400;
    }
    if ((code >= 0x0600) && (code <= 0x067F))   /* Arabic 0600 ~ 06FF */
    {
        return 0x0600;
    }
    if ((code >= 0x0900) && (code <= 0x097F))
    {
        return 0x0900;
    }
    if ((code >= 0x0980) && (code <= 0x09FF))   /* Bengali 0980 ~ 09FF */
    {
        return 0x0980;
    }
    if ((code >= 0x0A00) && (code <= 0x0A7F))   /* Punjabi 0A00 ~ 0A7F */
    {
        return 0x0A00;
    }
    if ((code >= 0x0A80) && (code <= 0x0AFF))   /* Gujarati 0A80 ~ 0AFF */
    {
        return 0x0A80;
    }
    if ((code >= 0x0B00) && (code <= 0x0B7F))   /* Oriya 0B00 ~ 0B7F */
    {
        return 0x0B00;
    }
    if ((code >= 0x0B80) && (code <= 0x0BFF))   /* Tamil 0B80 ~ 0BFF */
    {
        return 0x0B80;
    }
    if ((code >= 0x0C00) && (code <= 0x0C7F))   /* Telugu 0C00 ~ 0C7F */
    {
        return 0x0C00;
    }
    if ((code >= 0x0C80) && (code <= 0x0CFF))   /* Kannada 0C80 ~ 0CFF */
    {
        return 0x0C80;
    }
    if ((code >= 0x0D00) && (code <= 0x0D7F))   /* Malayalam 0D00 ~ 0D7F */
    {
        return 0x0D00;
    }
    if ((code >= 0x3100) && (code <= 0x312F))   /* BOPOMOFO 0x3100 ~ 0x312F */
    {
        return 0x3100;
    }

    return 0x0000;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_convert_to_0x81
 * DESCRIPTION
 *  This convert a 0x80 UCS2 string to 0x81 format. (Only for Thai Language right now.)
 * PARAMETERS
 *  input_name      [IN]        The input string.(Assume the input name is in high byte - low byte order)
 *  is_convert      [IN]        
 * RETURNS
 *  converted result length(in byte) for input string.
 *****************************************************************************/
U8 mmi_phb_convert_to_0x81(S8 *input_name, BOOL is_convert)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 code;
    U16 base = 0;
    BOOL flag = TRUE;
    S8 result_name[(MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH];
    S8 *temp_name;
    U8 i = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    temp_name = input_name;

    /* Find first UCS2 character as base */
    while (!((*temp_name == 0) && (*(temp_name + 1) == 0)))
    {
        memcpy(&code, temp_name, 2);

        /* Already GSM Encoding, need to exchange byte order */
        /*
         * if(is_convert)
         * {
         * code = (code << 8) | (code >> 8);
         * }
         */

        if (code >= 0x80 && UI_TEST_UCS2_CHARACTER(code))
        {
            base = mmi_phb_convert_to_0x81_get_base(code);
            break;
        }
        temp_name += 2;
    }

    /* Check if whole string can be encoded as 0x81. */
    temp_name = input_name;
    if (base > 0)
    {
        memset(result_name, 0xFF, (MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH);
        result_name[0] = (U8) 0x81;
        result_name[1] = (U8) mmi_ucs2strlen(input_name);
        result_name[2] = (U8) (base >> 7);

        i = 3;
        while (!((*temp_name == 0) && (*(temp_name + 1) == 0)))
        {
            memcpy(&code, temp_name, 2);

            /* Already GSM Encoding, need to exchange byte order */
            /*
             * if(is_convert)
             * {
             * code = (code << 8) | (code >> 8);
             * }
             */
            
            if (!UI_TEST_UCS2_CHARACTER(code))
            {
                if (UI_TEST_GSM_EXTENDED(code))
                {
                    flag = FALSE;
                    break;
                }
                else
                {
                    code = GSM7BitMapASCIIToDefaultByte(code);
                    result_name[i] = (U8)code;
                }
            }
            else if (mmi_phb_convert_to_0x81_get_base(code) != base)
            {
                flag = FALSE;
                break;
            }
            else
            {
                result_name[i] = mmi_phb_convert_to_0x81_get_offset(base, code);
            }
            temp_name += 2;
            i++;
        }

        if (flag)
        {
            /* Wrtie result to the same buffer */
            if (is_convert)
            {
                memcpy(input_name, result_name, (MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH);
            }
            return i;
        }
    }

    return 0;
}


/*****************************************************************************
 * FUNCTION
 *  PhbAsciiToGSM7Bit
 * DESCRIPTION
 *  Converts Ascii to GSM 7-Bit encoding
 * PARAMETERS
 *  buffer      [IN/OUT]        Result buffer
 * RETURNS
 *  void
 *****************************************************************************/
void PhbAsciiToGSM7Bit(PU8 buffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i, length;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    length = strlen((PS8) buffer);
    for (i = 0; i < length; ++i)
    {
        buffer[i] = GSM7BitMapASCIIToDefaultByte(buffer[i]);
    }
}


/*****************************************************************************
 * FUNCTION
 *  PhbGSM7BitToAscii
 * DESCRIPTION
 *  Converts GSM 7-Bit to Ascii encoding
 * PARAMETERS
 *  buffer      [IN/OUT]        Result buffer
 *  length      [IN]            Result buffer size
 * RETURNS
 *  void
 *****************************************************************************/
void PhbGSM7BitToAscii(PU8 buffer, U8 length)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 i;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (i = 0; i < length; ++i)
    {
        if (buffer[i] == 27)    /* treat escape char as a space */
        {
            buffer[i] = 32;
        }
        else
        {
            buffer[i] = GSM7BitMapDefaultToASCIIByte(buffer[i]);
        }
    }
}


/*****************************************************************************
 * FUNCTION
 *  GetUCS2Flag
 * DESCRIPTION
 *  Checks if the buffer contains Chinese character
 * PARAMETERS
 *  buffer      [IN]        String to be checked
 * RETURNS
 *  TRUE is a UCS2 two bytes string
 *****************************************************************************/
pBOOL GetUCS2Flag(PS8 buffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    pBOOL UCS2Flag = FALSE;
    U8 i, bufferLen;
    UI_character_type ch;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    bufferLen = mmi_ucs2strlen(buffer) * ENCODING_LENGTH;
    for (i = 1; i < bufferLen; i += 2)
    {
        ch = ((UI_character_type) buffer[i] << 8) | ((U8) buffer[i - 1]);
        if (UI_TEST_UCS2_CHARACTER(ch))
        {
            UCS2Flag = TRUE;
            break;
        }
    }
    return UCS2Flag;
}


/*****************************************************************************
 * FUNCTION
 *  GetUCS2ExtendedNum
 * DESCRIPTION
 *  Checks if the buffer contains Chinese character
 * PARAMETERS
 *  buffer      [IN]        String to be checked
 * RETURNS
 *  TRUE is a UCS2 two bytes string
 *****************************************************************************/
U8 GetUCS2ExtendedNum(PS8 buffer, U8 max_buffer_len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 extend_num = 0;
    UI_character_type ch;
    U8 ex_count = 0;
    U8 ex_len = 1; /* need second bytes */
    U8 buffer_len = mmi_ucs2strlen(buffer) * ENCODING_LENGTH;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (ex_count < max_buffer_len && ex_len < buffer_len)
    {
        ch = ((UI_character_type) buffer[ex_len] << 8) | ((UI_character_type) buffer[ex_len - 1]);
        if (UI_TEST_GSM_EXTENDED(ch))
        {
            ex_count++;
            extend_num++;
        }
        ex_count++;
        ex_len += 2;
    }
    return extend_num;
}


/*****************************************************************************
 * FUNCTION
 *  BigEndianToLittleEndian
 * DESCRIPTION
 *  Changes big endian to be little endian and vice versa
 * PARAMETERS
 *  dstBuffer       [IN]        Destination buffer
 *  srcBuffer       [IN]        Source buffer
 * RETURNS
 *  result string
 *****************************************************************************/
PS8 BigEndianToLittleEndian(PS8 dstBuffer, PS8 srcBuffer)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 i = 0;
    U8 len;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    len = mmi_ucs2strlen(srcBuffer) * ENCODING_LENGTH;
    while (i != len)
    {
        dstBuffer[i] = srcBuffer[i + 1];
        dstBuffer[i + 1] = srcBuffer[i];
        i += 2;
    }
    dstBuffer[i] = '\0';
    dstBuffer[i + 1] = '\0';

    return dstBuffer;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_check_is_phonebook_full
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  
 *****************************************************************************/
U8 mmi_phb_check_is_phonebook_full(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_DUAL_SIM_MASTER__
    if (PhoneBookEntryCount == g_phb_cntx.sim_total + g_phb_cntx.phone_total + MTPNP_AD_ADN_SIM2GetCapacity())
#else /* __MMI_DUAL_SIM_MASTER__ */
    if(PhoneBookEntryCount == (g_phb_cntx.sim_total + g_phb_cntx.phone_total))
#endif /* __MMI_DUAL_SIM_MASTER__ */
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}


#if defined(__MMI_UCM__)
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_get_caller_image_info
 * DESCRIPTION
 *  Phonebook list interface for other application.
 * PARAMETERS
 *  func_list       [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_get_caller_image_info(const mmi_phb_caller_image_input_struct* img_input, mmi_phb_caller_image_info_struct* image_info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S8 *path;
    FS_HANDLE fh;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    /* reset the image_info struct */
#if defined(__MMI_VIDEO_TELEPHONY__)
    if (img_input->call_type == MMI_PHB_VIDEO_CALL_TYPE)
    {
        image_info->new_image_id = IMG_PHB_VIDEO_DEFAULT;
    }
    else
#endif
    {
        image_info->new_image_id = IMG_PHB_DEFAULT;
    }
    image_info->image_type = 0;
    image_info->is_video_sound = MMI_FALSE;
	memset(image_info->image_path, 2, 0);
  
#ifdef __MMI_INCOMING_CALL_VIDEO__
    if ((img_input->video_id & 0x8000) || ((img_input->video_id & 0x3fff) >= VDO_ID_PHB_MTCALL_1))
    {
        /* use video for display */
        /* user defined video file or system default video resource */

        if (img_input->video_id & 0x4000)
        {
            image_info->is_video_sound = MMI_TRUE;
        }
        if (img_input->video_id & 0x8000) /* video file, including file, swflash and avatar */
        {
            if (img_input->video_record_index != 0)
            {
                path = mmi_phb_video_get_path_from_id(img_input->video_record_index);
                if (path == NULL)
                {
                    image_info->is_video_sound = MMI_FALSE;
                    /* play default pic */
                    return;
                }
                else
                {
                #ifdef __MMI_AVATAR__
                    if (img_input->video_id & 0x1000)
                    {
                        image_info->image_type = WGUI_CATE_MOMT_RES_TYPE_AVATAR_FILE;
                        image_info->new_image_id = 0;
                        image_info->is_video_sound = MMI_FALSE; /* avatar has no sound */
                        image_info->image_path[0] = ((U32)path)>>24;
                        image_info->image_path[1] = ((U32)path)>>16;
                        image_info->image_path[2] = ((U32)path)>>8;
                        image_info->image_path[3] = ((U32)path)&0xff;
                        image_info->image_path[4] = 0;
                        return;
                    }
                    else
                #endif                    
                    {                
                        fh = FS_Open((PU16) path, FS_READ_ONLY);
                    
                        if (fh > FS_NO_ERROR)
                        {  
                            FS_Close(fh);
                            mmi_ucs2ncpy(image_info->image_path, path, FMGR_MAX_PATH_LEN);
                        #ifdef __MMI_SWFLASH__
                            if (img_input->video_id & 0x2000)
                            {
                                image_info->image_type = WGUI_CATE_MOMT_RES_TYPE_SWFLASH_FILE;
                                image_info->new_image_id = 0;
                                return;
                            }
                            else
                        #endif
                            {
                                image_info->image_type = WGUI_CATE_MOMT_RES_TYPE_VIDEO_FILE;
                                image_info->new_image_id = 0;
                                return;
                            }
                                                   
                        }
                        else
                        {
                            image_info->is_video_sound = MMI_FALSE;
                            return;
                        }
                    }
                }
            }
            else
            {
                image_info->is_video_sound = MMI_FALSE;
                return;                
            }
        }
        else /* video 1, swflash 1 */
        {
            /* resource */
            image_info->new_image_id = img_input->video_id & 0x3fff;
        #ifdef __MMI_SWFLASH__ /* Check it's default sw flash or video */
            if (image_info->new_image_id >= MFH_ID_PHB_MTCALL_1)
            {
                image_info->image_type = WGUI_CATE_MOMT_RES_TYPE_SWFLASH_ID;
                return;
            }
            else
        #endif /* __MMI_SWFLASH__ */
            {
                image_info->image_type = WGUI_CATE_MOMT_RES_TYPE_VIDEO_ID;
                return;
            }        
        }
    }
    else
#endif /* def __MMI_INCOMING_CALL_VIDEO__ */    
#if defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__)
    if (img_input->image_id == 1) // image from file
    {
        if (img_input->record_index != 0)
        {
            path = mmi_phb_image_get_path_from_id(img_input->record_index);
            if (path != NULL)
            {
                fh = FS_Open((PU16) path, FS_READ_ONLY);

                if (fh > FS_NO_ERROR)
                {  
                    FS_Close(fh);
                    mmi_ucs2ncpy(image_info->image_path, path, FMGR_MAX_PATH_LEN);
                    image_info->image_type = WGUI_CATE_MOMT_RES_TYPE_IMAGE_FILE;
                    image_info->new_image_id = 0;
                }
            }
            return;
        }
        return;
    }
    else
#endif /* defined(__MMI_PHB_CALL_SHOW_PICTURE_FROM_FILE__) */
    {
        if (img_input->image_id == IMG_PHB_DEFAULT || img_input->image_id == 0)
        {
            return;
        }
        else
        {
            image_info->new_image_id = img_input->image_id;
            return;
        }
    }
}
#endif /* defined(__MMI_UCM__) */


#if defined(__MMI_IMPS__) && defined(IMPS_WV13_ENHANCEMENT) && defined(__MMI_VCARD__)
void mmi_phb_pre_entry_imps_send_vcard(mmi_vcard_send_by_imps_cbk_type send_vcard_cb)
{
    phb_list_view_type = MMI_PHB_ENTRY_FIELD_NAME;
    ASSERT(send_vcard_cb != NULL);
    g_vcard_send_by_imps_cb = send_vcard_cb;
#ifdef __MMI_PHB_QUICK_SEARCH__
    phbListView = MMI_PHB_LIST_FOR_IMPS;
    if (IsScreenPresent(SCR_ID_PHB_GENERIC_QUICK_SEARCH_LIST))
    {
        DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_QUICK_SEARCH_LIST);
    }
    mmi_phb_entry_generic_quick_search_list();
#else
    mmi_phb_list_entry_for_number_and_email();
#endif
}
#endif /* defined(__MMI_IMPS__) && defined(IMPS_WV13_ENHANCEMENT) */


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_compare_numbers
 * DESCRIPTION
 *  check two phonebook number equals
 * PARAMETERS
 *  number1       [IN]
 *  number2		  [IN]
 * RETURNS
 *  MMI_BOOL
 *****************************************************************************/
MMI_BOOL mmi_phb_compare_numbers(S8 *number1, S8 *number2)
{
	/*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
	MMI_BOOL result = FALSE;
#ifndef __MMI_PHB_ADV_NUM_MATCH_MOBILE__
	U32 num1, num2;
#endif /* __MMI_PHB_ADV_NUM_MATCH_MOBILE__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if defined(__MMI_PHB_ADV_NUM_MATCH_MOBILE__)
	result = mmi_phb_number_compare_before_extension(number1, number2);

	if (result)
	{
		result = mmi_phb_number_compare_extension(number1, number2);
	}
#else	
	num1 = mmi_phb_util_convert_number_to_int((U8*)number1);
	num2 = mmi_phb_util_convert_number_to_int((U8*)number2);
	
	if (num1 == num2)
	{
            result = TRUE;
	}		
#endif /* defined(__MMI_PHB_ADV_NUM_MATCH_MOBILE__) */ 

	return result;
}


#ifdef __MMI_URI_AGENT__
void mmi_phb_uriagent_wtai_save_entry(mmi_uriagent_appid_enum appid, char *url, char *param, mmi_uriagent_options_enum options, mmi_uriagent_uri_request_hdlr_cb cb);
mmi_phb_uriagent_err_enum mmi_phb_uriagent_parse_uri(U8* uri, S8* uc2_name, S8* ucs2_num);
void mmi_phb_uriagent_exec_callback(mmi_uriagent_uri_request_hdlr_cb cb, mmi_uriagent_appid_enum ura_appid, mmi_uriagent_appid_enum uha_appid, U32 result);


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_init_uri_agent_registration
 * DESCRIPTION
 *  Initialize URI agent registration
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_init_uri_agent_registration(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_uriagent_err_enum err_code = MMI_URIAGENT_ERR_UNSUPPORTED;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_INIT_URI_AGENT_REGISTERATION);
    err_code = mmi_uriagent_register_hdlr_by_prefix("wtai://wp/ap", mmi_phb_uriagent_wtai_save_entry, MMI_FALSE);

    if (err_code != MMI_URIAGENT_ERR_OK)
    {
        MMI_ASSERT(0);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_uriagent_wtai_save_entry
 * DESCRIPTION
 *  main execute function for phb UHA
 * PARAMETERS
 *  appid
 *  url
 *  param
 *  options: here is useless in phb, because phb will always popup confirm to user to check if save?
 *  cb
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_uriagent_wtai_save_entry(mmi_uriagent_appid_enum appid, char *url, char *param, mmi_uriagent_options_enum options, mmi_uriagent_uri_request_hdlr_cb cb)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_phb_uriagent_err_enum parse_result = MMI_PHB_URIAGENT_ERR_OK;
    S8 ucs2_num_uri[(MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH];
    S8 ucs2_name_uri[(MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_URIAGENT_WTAI_SAVE_ENTRY, appid, options, cb);
    MMI_PRINT(MOD_MMI_COMMON_APP, MMI_PHB_TRACE_GROUP,"[PHB] mmi_phb_uriagent_wtai_save_entry(),  uri:%s", url);

#ifdef __MMI_DUAL_SIM_MASTER__
    if (MTPNP_PFAL_Phb_IsReady() == MTPNP_FALSE)
#else   /* __MMI_DUAL_SIM_MASTER__ */
    if (!g_phb_cntx.phb_ready || g_phb_cntx.processing)
#endif  /* __MMI_DUAL_SIM_MASTER__ */
    {
        parse_result = MMI_PHB_URIAGENT_ERR_PHB_NOT_READY;
    }
#ifdef __MMI_PHB_USIM_FIELD__
    else if (g_phb_cntx.is_usim && g_phb_cntx.usim_ready_stage != MMI_PHB_USIM_READY)
    {
        parse_result = MMI_PHB_URIAGENT_ERR_PHB_PROCESS_USIM;
    }
#endif /* __MMI_PHB_USIM_FIELD__ */
#ifdef __SYNCML_SUPPORT__
    else if (mmi_syncml_is_phb_sync_now())
    {
        parse_result = MMI_PHB_URIAGENT_ERR_PHB_IN_SYNC;
    }
#endif /* __SYNCML_SUPPORT__ */
#ifdef __MMI_DUAL_SIM_MASTER__
    else if (PhoneBookEntryCount == g_phb_cntx.sim_total + g_phb_cntx.phone_total + MTPNP_AD_ADN_SIM2GetCapacity())
#else   /* __MMI_DUAL_SIM_MASTER__ */
    else if (PhoneBookEntryCount == (g_phb_cntx.sim_total + g_phb_cntx.phone_total))
#endif  /* __MMI_DUAL_SIM_MASTER__ */
    {
        parse_result = MMI_PHB_URIAGENT_ERR_PHB_IS_FULL;
    }

    if (parse_result != MMI_PHB_URIAGENT_ERR_OK)
    {
        mmi_phb_uriagent_display_popup((U8)parse_result);
        mmi_phb_uriagent_exec_callback(cb, appid, MMI_URIAGENT_APPID_PHONEBOOK, parse_result);
        return;
    }
    
    memset(ucs2_num_uri, 0, (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
    memset(ucs2_name_uri, 0, (MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH);
    parse_result = mmi_phb_uriagent_parse_uri((U8*)url, ucs2_name_uri, ucs2_num_uri);

    if (!(parse_result == MMI_PHB_URIAGENT_ERR_OK || parse_result == MMI_PHB_URIAGENT_ERR_NAME_TOO_LONG || 
        parse_result == MMI_PHB_URIAGENT_ERR_NUM_TOO_LONG || parse_result == MMI_PHB_URIAGENT_ERR_URI_FORMAT_WRONG))
        ASSERT(0);
      
    if (parse_result != MMI_PHB_URIAGENT_ERR_OK)
    {
        mmi_phb_uriagent_display_popup((U8)parse_result);
        mmi_phb_uriagent_exec_callback(cb, appid, MMI_URIAGENT_APPID_PHONEBOOK, parse_result);
        return;
    }

    // fill the name and number field
    g_phb_cntx.set_done_flag = 1;
    mmi_phb_clear_old_history();
    mmi_phb_util_clear_buffer(TRUE);

    if (mmi_ucs2strlen(ucs2_num_uri))
    {
        mmi_ucs2ncpy(pbNumber, ucs2_num_uri, (MAX_PB_NUMBER_LENGTH + 1));
    }

    if (mmi_ucs2strlen(ucs2_name_uri))
    {        	
        mmi_ucs2ncpy(pbName, ucs2_name_uri, (MAX_PB_NAME_LENGTH + 1));
        mmi_phb_truncate_pbname_buffer(MMI_NVRAM);
    }
    g_phb_enter_from = MMI_PHB_ENTER_FROM_URIAGENT;
    mmi_phb_uriagent_ura_appid = appid;
    mmi_phb_uriagent_callback = cb;
    mmi_phb_entry_op_add_choose_storage();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_uriagent_exec_callback
 * DESCRIPTION
 *  Execute uriagent callback
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_uriagent_exec_callback(mmi_uriagent_uri_request_hdlr_cb cb, mmi_uriagent_appid_enum ura_appid, mmi_uriagent_appid_enum uha_appid, U32 result)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_URIAGENT_EXEC_CALLBACK, cb, ura_appid, uha_appid, result);

    if (cb != NULL)
    {
        cb(ura_appid, uha_appid, (unsigned int)result);
    }
    return;
}



/*****************************************************************************
 * FUNCTION
 *  mmi_phb_uriagent_result_callback
 * DESCRIPTION
 *  Execute uriagent callback for phb after add contact
 * PARAMETERS
 *  uriagent_result
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_uriagent_result_callback(U8 uriagent_result)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_URIAGENT_RESULT_CALLBACK, uriagent_result);
    mmi_phb_uriagent_exec_callback(mmi_phb_uriagent_callback, mmi_phb_uriagent_ura_appid, MMI_URIAGENT_APPID_PHONEBOOK, (mmi_phb_uriagent_err_enum)uriagent_result);
    mmi_phb_uriagent_callback = NULL;
}



/*****************************************************************************
 * FUNCTION
 *  mmi_phb_uriagent_display_popup
 * DESCRIPTION
 *  Display popup about the information of phb UHA for uriagent
 * PARAMETERS
 *  U8 result
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_uriagent_display_popup(U8 result)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_URIAGENT_DISPLAY_POPUP, result);

    switch ((mmi_phb_uriagent_err_enum)result)
    {
        case MMI_PHB_URIAGENT_ERR_OK:
        {
            mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_SAVED), MMI_EVENT_SUCCESS);
            break;
        }

        case MMI_PHB_URIAGENT_ERR_PHB_NOT_READY:
        {
            mmi_phb_entry_not_ready(STR_PROCESSING_PHONEBOOK);
            break;
        }

    #ifdef __MMI_PHB_USIM_FIELD__
        case MMI_PHB_URIAGENT_ERR_PHB_PROCESS_USIM:
        {
            mmi_phb_entry_not_ready(STR_ID_PHB_PROCESSING_USIM);
            break;
        }
    #endif

    #ifdef __SYNCML_SUPPORT__
        case MMI_PHB_URIAGENT_ERR_PHB_IN_SYNC:
        {
            mmi_phb_entry_not_ready(STR_ID_SYNC_PLEASE_WAIT);
            break;
        }
    #endif

        case MMI_PHB_URIAGENT_ERR_URI_FORMAT_WRONG:
        {
            mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_INVALID_FORMAT), MMI_EVENT_FAILURE);
            break;
        }
        
        case MMI_PHB_URIAGENT_ERR_NAME_TOO_LONG:
        {
            mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_URIAGENT_NAME_TOO_LONG), MMI_EVENT_FAILURE);
            break;
        }

        case MMI_PHB_URIAGENT_ERR_NUM_TOO_LONG:
        {
            mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_URIAGENT_NUMBER_TOO_LONG), MMI_EVENT_FAILURE);
            break;
        }

        case MMI_PHB_URIAGENT_ERR_PHB_WRITE_ERROR:
        {
            mmi_display_popup((UI_string_type)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE);
            break;
        }

        case MMI_PHB_URIAGENT_ERR_FDN_ON_ERROR:
        {
            mmi_display_popup((UI_string_type)GetString(STR_FDL_IS_ON), MMI_EVENT_FAILURE);
            break;
        }

        case MMI_PHB_URIAGENT_ERR_PHB_IS_FULL:
        {
            mmi_display_popup((UI_string_type)GetString(STR_PBOOK_FULL_MSG), MMI_EVENT_FAILURE);
            break;
        }

        default:
        {
            MMI_ASSERT(0);
            break;
        }
    };
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_uriagent_parse_uri
 * DESCRIPTION
 *  Parse URL passed by uri agent
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
mmi_phb_uriagent_err_enum mmi_phb_uriagent_parse_uri(U8* uri, S8* ucs2_name, S8* ucs2_num)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S8* cur_pos = (S8*)uri;
    S8* num_start_pos = NULL;
    S8* name_start_pos = NULL;
    U16 num_len, ucs2_num_len, ucs2_name_len;
    S8* temp_num_buff = NULL;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_PRINT(MOD_MMI_COMMON_APP, MMI_PHB_TRACE_GROUP,"[PHB] mmi_phb_uriagent_parse_uri(),  uri:%s", uri);

    cur_pos = strstr((S8*)uri, ";");

    /* number */
    if (cur_pos == NULL)
    {
        return MMI_PHB_URIAGENT_ERR_URI_FORMAT_WRONG;
    }
    else
    {
        num_start_pos = cur_pos + 1;
    }

    cur_pos = strstr(num_start_pos, ";");
    if (cur_pos == NULL)
    {
        // should handle 
        return MMI_PHB_URIAGENT_ERR_URI_FORMAT_WRONG;
    }
    else
    {
        name_start_pos = cur_pos + 1;
        ucs2_name_len = mmi_charset_utf8_to_ucs2_length_in_bytes((const kal_uint8*)name_start_pos);

        if (ucs2_name_len > MAX_PB_NAME_LENGTH  * ENCODING_LENGTH)
        {
            return MMI_PHB_URIAGENT_ERR_NAME_TOO_LONG;
        }
        
        num_len = name_start_pos - num_start_pos - 1;
        temp_num_buff = OslMalloc(num_len + 1);
        memset(temp_num_buff, 0x00, (num_len + 1));
        memcpy(temp_num_buff, num_start_pos, num_len);
        ucs2_num_len = mmi_charset_utf8_to_ucs2_length_in_bytes((const kal_uint8*)temp_num_buff);
    }
    
    if (*num_start_pos == '+')
    {
        if (ucs2_num_len > ((MAX_PB_NUMBER_LENGTH + 1) * ENCODING_LENGTH))
        {
            OslMfree(temp_num_buff);
            return MMI_PHB_URIAGENT_ERR_NUM_TOO_LONG;
        }
        else
        {
            mmi_chset_convert(
                MMI_CHSET_UTF8,
                MMI_CHSET_UCS2,
                (char*)temp_num_buff,
                (char*)ucs2_num,
                (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
        }
    }
    else
    {
        if (ucs2_num_len > MAX_PB_NUMBER_LENGTH  * ENCODING_LENGTH)
        {
            OslMfree(temp_num_buff);
            return MMI_PHB_URIAGENT_ERR_NUM_TOO_LONG;
        }
        else
        {
            mmi_chset_convert(
                MMI_CHSET_UTF8,
                MMI_CHSET_UCS2,
                (char*)temp_num_buff,
                (char*)ucs2_num,
                (MAX_PB_NUMBER_LENGTH + 1) * ENCODING_LENGTH);
        }
    }

    mmi_chset_convert(
        MMI_CHSET_UTF8,
        MMI_CHSET_UCS2,
        (char*)name_start_pos,
        (char*)ucs2_name,
        (MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH);
        
    OslMfree(temp_num_buff);
    return MMI_PHB_URIAGENT_ERR_OK;
}



/*****************************************************************************
 * FUNCTION
 *  mmi_phb_uriagent_choose_storage_delete_cb
 * DESCRIPTION
 *  Delete callback of choose storage screen
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
U8 mmi_phb_uriagent_choose_storage_delete_cb(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_URIAGENT_CHOOSE_STORAGE_DELETE_CB);

    mmi_phb_uriagent_result_callback(MMI_PHB_URIAGENT_ERR_PHB_WRITE_ERROR);
    return MMI_FALSE;
}
#endif /* __MMI_URI_AGENT__ */


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_nlf_get_sorted_index
 * DESCRIPTION
 *  replace contact for unsaved data
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
U16 mmi_phb_nlf_get_sorted_index(U16 nIndex, U8 storage)
{
    U16 store_index = 0;
    S32 i = -1;
    S32 inner_index = 0;
    U16 sorted_index = 0;

    while (i < nIndex)
    {
        store_index = g_phb_name_index[inner_index];
    #ifdef __MMI_DUAL_SIM_MASTER__
        if (storage == MMI_SIM)
        {
            if ((store_index >= MAX_PB_PHONE_ENTRIES)&&(store_index < (MAX_PB_PHONE_ENTRIES+MAX_PB_SIM_ENTRIES)))
            {
                i++;
            }
        }
        else if(storage == MMI_NVRAM)
        {
            if(store_index < MAX_PB_PHONE_ENTRIES)
            {
                i++;
            }
        }
        else if (storage == MMI_SIM2)
        {
            if(store_index >= (MAX_PB_PHONE_ENTRIES+MAX_PB_SIM_ENTRIES))
            {
                i++;
            }
        }
    #else/* __MMI_DUAL_SIM_MASTER__ */
        if (storage == MMI_SIM)
        {
            if (store_index >= MAX_PB_PHONE_ENTRIES)
            {
                i++;
            }
        }
        else if (storage == MMI_NVRAM)
        {
            if(store_index < MAX_PB_PHONE_ENTRIES)
            {
                i++;
            }
        }
#endif/* __MMI_DUAL_SIM_MASTER__ */
        else
        {
            i++;
        }
        inner_index++;
    }

    sorted_index = (U16) (inner_index - 1);
    return sorted_index;
}


#ifdef __MMI_PHB_SAVE_CONTACT_NOTIFY__
/* function of operation on unsaved data link list start */
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_free_node
 * DESCRIPTION
 *  free node function for unsaved data link list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_unsaved_data_free_node(mmi_phb_unsaved_data_node_struct *data_node)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(data_node != NULL);
    /* free all fields in phb_data */
    if (data_node->object->phb_data->name != NULL)
    {
        OslMfree(data_node->object->phb_data->name);
    }
    if (data_node->object->phb_data->number != NULL)
    {
        OslMfree(data_node->object->phb_data->number);
    }
#if defined(__MMI_PHB_INFO_FIELD__)
    if (data_node->object->phb_data->pobox != NULL)
    {
        OslMfree(data_node->object->phb_data->pobox);
    }

    if (data_node->object->phb_data->extension != NULL)
    {
        OslMfree(data_node->object->phb_data->extension);
    }

    if (data_node->object->phb_data->street != NULL)
    {
        OslMfree(data_node->object->phb_data->street);
    }

    if (data_node->object->phb_data->city != NULL)
    {
        OslMfree(data_node->object->phb_data->city);
    }

    if (data_node->object->phb_data->state != NULL)
    {
        OslMfree(data_node->object->phb_data->state);
    }

    if (data_node->object->phb_data->postalcode != NULL)
    {
        OslMfree(data_node->object->phb_data->postalcode);
    }

    if (data_node->object->phb_data->country != NULL)
    {
        OslMfree(data_node->object->phb_data->country);
    }
#endif /* defined(__MMI_PHB_INFO_FIELD__) */
    /* free other fields */
    if (data_node->object->phb_data != NULL)
    {
        OslMfree(data_node->object->phb_data);
    }
    if (data_node->object != NULL)
    {
        OslMfree(data_node->object);
    }
    OslMfree(data_node);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_init_node
 * DESCRIPTION
 *  init a node function for unsaved data link list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
struct mmi_phb_unsaved_data_node_struct* mmi_phb_unsaved_data_init_node(MMI_PHB_STORAGE_LOCATION storage, mmi_phb_fields_type_enum show_type, MMI_PHB_FIELDS_STRUCT *phb_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    struct mmi_phb_unsaved_data_node_struct *data_node;
    U8 field_len;
    U8 number_UCS2[(MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH];
    U8 i;
    U8 num_plus_index = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    data_node = OslMalloc(sizeof(mmi_phb_unsaved_data_node_struct)); 
    if (data_node == NULL)
    {
        /* err handler */
    }
    else
    {
        memset(data_node, 0x00, sizeof(mmi_phb_unsaved_data_node_struct));
        data_node->object = OslMalloc(sizeof(mmi_phb_unsaved_data_struct));
        memset(data_node->object, 0x00, sizeof(mmi_phb_unsaved_data_struct));

        data_node->object->storage = storage;
        data_node->object->field_type = show_type;
        data_node->object->phb_data = OslMalloc(sizeof(MMI_PHB_FIELDS_STRUCT));
        memset(data_node->object->phb_data, 0x00, sizeof(MMI_PHB_FIELDS_STRUCT));
        if (phb_data->name != NULL && (field_len = mmi_ucs2strlen(phb_data->name)))
        {
            data_node->object->phb_data->name = OslMalloc((MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH);
            memset(data_node->object->phb_data->name, 0x00, (MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH);
            mmi_ucs2ncpy(data_node->object->phb_data->name, phb_data->name, (field_len < MAX_PB_NAME_LENGTH ? field_len : MAX_PB_NAME_LENGTH));
        }

        if (phb_data->number != NULL && (field_len = mmi_ucs2strlen(phb_data->number)))
        {            
            memset(number_UCS2, 0, (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);

            if (field_len > MAX_PB_NUMBER_LENGTH)
            {
                if (phb_data->number[0] == '+')
                {
                    number_UCS2[0] = phb_data->number[0];
                    num_plus_index++;
                }

                for (i = 0; i < MAX_PB_NUMBER_LENGTH; i++)
                {
                    number_UCS2[2 * (i + num_plus_index)] = phb_data->number[2 * (field_len - MAX_PB_NUMBER_LENGTH - 1 + i + num_plus_index)];
                    number_UCS2[2 * (i + num_plus_index) + 1] = phb_data->number[2 * (field_len - MAX_PB_NUMBER_LENGTH - 1 + i + num_plus_index) + 1];
                }
            }
            else
            {
                mmi_ucs2ncpy((S8*) number_UCS2, phb_data->number, MAX_PB_NUMBER_LENGTH);
            }

            data_node->object->phb_data->number = OslMalloc((MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
            memset(data_node->object->phb_data->number, 0x00, (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
            mmi_ucs2ncpy(data_node->object->phb_data->number, (S8*) number_UCS2, MAX_PB_NUMBER_LENGTH + 1);
        }

    #if defined(__MMI_PHB_INFO_FIELD__)
        if (phb_data->pobox != NULL && (field_len = mmi_ucs2strlen(phb_data->pobox)))
        {
            data_node->object->phb_data->pobox = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            memset(data_node->object->phb_data->pobox, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            mmi_ucs2ncpy(data_node->object->phb_data->pobox, phb_data->pobox, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
        }

        if (phb_data->extension != NULL && (field_len = mmi_ucs2strlen(phb_data->extension)))
        {
            data_node->object->phb_data->extension = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            memset(data_node->object->phb_data->extension, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            mmi_ucs2ncpy(data_node->object->phb_data->extension, phb_data->extension, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
        }

        if (phb_data->street != NULL && (field_len = mmi_ucs2strlen(phb_data->street)))
        {
            data_node->object->phb_data->street = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            memset(data_node->object->phb_data->street, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            mmi_ucs2ncpy(data_node->object->phb_data->street, phb_data->street, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
        }

        if (phb_data->city != NULL && (field_len = mmi_ucs2strlen(phb_data->city)))
        {
            data_node->object->phb_data->city = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            memset(data_node->object->phb_data->city, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            mmi_ucs2ncpy(data_node->object->phb_data->city, phb_data->city, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
        }

        if (phb_data->state != NULL && (field_len = mmi_ucs2strlen(phb_data->state)))
        {
            data_node->object->phb_data->state = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            memset(data_node->object->phb_data->state, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            mmi_ucs2ncpy(data_node->object->phb_data->state, phb_data->state, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
        }

        if (phb_data->postalcode != NULL && (field_len = mmi_ucs2strlen(phb_data->postalcode)))
        {
            data_node->object->phb_data->postalcode = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            memset(data_node->object->phb_data->postalcode, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            mmi_ucs2ncpy(data_node->object->phb_data->postalcode, phb_data->postalcode, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
        }

        if (phb_data->country != NULL && (field_len = mmi_ucs2strlen(phb_data->country)))
        {
            data_node->object->phb_data->country = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            memset(data_node->object->phb_data->country, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
            mmi_ucs2ncpy(data_node->object->phb_data->country, phb_data->country, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
        }
    #endif /* defined(__MMI_PHB_INFO_FIELD__) */
    }
    return data_node;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_add_node
 * DESCRIPTION
 *  add one node to the unsaved data link list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_unsaved_data_add_node(struct mmi_phb_unsaved_data_node_struct* new_node)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (unsaved_data_head == NULL)
    {
        unsaved_data_head = new_node;
    }
    if (unsaved_data_end == NULL)
    {
        unsaved_data_end = new_node;
    }
    unsaved_data_end->next = new_node;
    new_node->next = NULL;
    unsaved_data_end = new_node;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_search_node
 * DESCRIPTION
 *  search the ith node in the unsaved data link list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
struct mmi_phb_unsaved_data_node_struct* mmi_phb_unsaved_data_search_node(U8 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    struct mmi_phb_unsaved_data_node_struct *search_node;
    U8 i = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(index < g_phb_unsaved_data_num);
    search_node = unsaved_data_head;
    while(i < index)
    {
        search_node = search_node->next;
        i++;
    }

    ASSERT(search_node != NULL);
    return search_node;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_delete_node
 * DESCRIPTION
 *  delete the whole unsaved data link list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_unsaved_data_delete_node(struct mmi_phb_unsaved_data_node_struct* node)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    struct mmi_phb_unsaved_data_node_struct *prev, *temp;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    temp = node;
    prev = unsaved_data_head;

    if (prev == temp)
    {
        unsaved_data_head = unsaved_data_head->next;
        if (unsaved_data_end == temp)
        {
            unsaved_data_end = unsaved_data_end->next;
        }
        mmi_phb_unsaved_data_free_node(node);
    }
    else
    {
        while(prev->next != temp)
        {
            prev = prev->next;
        }
        prev->next = temp->next;
        if (unsaved_data_end == temp)
        {
            unsaved_data_end = prev;
        }
        mmi_phb_unsaved_data_free_node(node);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_delete_list
 * DESCRIPTION
 *  delete the whole unsaved data link list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_unsaved_data_delete_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    struct mmi_phb_unsaved_data_node_struct *ptr, *temp;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (unsaved_data_head == NULL) return;

    ptr = unsaved_data_head;
    while(ptr != NULL)
    {
        temp = ptr;
        ptr = ptr->next;
        mmi_phb_unsaved_data_free_node(temp);
    }
    unsaved_data_head = NULL;
    unsaved_data_end = NULL;
}
/* function of operation on unsaved data link list end */


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_is_save_contact_notify_on
 * DESCRIPTION
 *  Check if the save contact notify on
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
MMI_BOOL mmi_phb_is_save_contact_notify_on(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_IS_SAVE_CONTACT_NOTIFY_ON, g_phb_cntx.save_contact_notify);

#ifdef __SYNCML_SUPPORT__
    if (mmi_syncml_is_phb_sync_now())
    {
        return MMI_FALSE;
    }
#endif /* __SYNCML_SUPPORT__ */

#ifdef __MMI_DUAL_SIM_MASTER__
    if (MTPNP_PFAL_Phb_IsReady() == MTPNP_FALSE)
#else /* __MMI_DUAL_SIM_MASTER__ */
    if (!g_phb_cntx.phb_ready || g_phb_cntx.processing)
#endif /* __MMI_DUAL_SIM_MASTER__ */
    {
        return MMI_FALSE;
    }

    if (mmi_secset_is_in_puk_check() == MMI_TRUE)
    {
        return MMI_FALSE;
    }
    return (g_phb_cntx.save_contact_notify == MMI_PHB_SAVE_CONTACT_NOTIFY_ON ? MMI_TRUE : MMI_FALSE);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_check_if_data_duplicated
 * DESCRIPTION
 *  Check if the unsaved data is duplicated in the unsaved list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
MMI_BOOL mmi_phb_check_if_data_duplicated(U8* data, U8 data_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 index;
    MMI_BOOL result = MMI_FALSE;
    U8 cond_number_ASCII[MAX_PB_NUMBER_LENGTH + 1 + 1];
    U8 list_number_ASCII[MAX_PB_NUMBER_LENGTH + 1 + 1];
    mmi_phb_unsaved_data_node_struct* contact_node;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (index = 0; index < g_phb_unsaved_data_num; index++)
    {
        contact_node = mmi_phb_unsaved_data_search_node(index);

        if (data_type == MMI_PHB_FIELDS_TYPE_NUMBER)
        {
            mmi_ucs2_to_asc((S8*) cond_number_ASCII, (S8*) data);
            mmi_ucs2_to_asc((S8*) list_number_ASCII, (S8*) contact_node->object->phb_data->number);

            if (!mmi_phb_number_has_extension((S8*) list_number_ASCII))
            {
                if (mmi_phb_number_compare_before_extension((S8*) cond_number_ASCII, (S8*) list_number_ASCII))
                {
                    return MMI_TRUE;
                }
            }
            else
            {
                if (mmi_phb_number_compare_before_extension((S8*) cond_number_ASCII, (S8*) list_number_ASCII) &&
                mmi_phb_number_compare_extension((S8*) cond_number_ASCII, (S8*) list_number_ASCII))
                {
                    return MMI_TRUE;
                }
            }
        }
    }
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_CHECK_IF_DATA_DUPLICATED, result);

    return result;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_is_num_in_phb
 * DESCRIPTION
 *  Check if the num is stored in phb or not
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
MMI_BOOL mmi_phb_is_num_in_phb(U8* ucs2_num)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_BOOL result;
    U32 num;
    U8 number_ASCII[MAX_PB_NUMBER_LENGTH + 1 + 1];
    U8 number_UCS2[(MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(g_phb_cntx.save_contact_notify == MMI_PHB_SAVE_CONTACT_NOTIFY_ON);

    /* check if number could be transfer to valid num (int) */
    memset(number_ASCII, 0, (MAX_PB_NUMBER_LENGTH + 1 + 1));
    memset(number_UCS2, 0, (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);

    mmi_phb_truncate_number_field_to_max_len(number_UCS2, ucs2_num);
    mmi_ucs2_n_to_asc((PS8) number_ASCII, (PS8) number_UCS2, (MAX_PB_NUMBER_LENGTH + 1));
    num = mmi_phb_util_convert_number_to_int(number_ASCII);

    MMI_PRINT(MOD_MMI_COMMON_APP, MMI_PHB_TRACE_GROUP,"[PHB] mmi_phb_is_num_in_phb(),  number:%s", number_ASCII);

    if (num == INVALID_NUMBER)
    {
        MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_IS_NUM_IN_PHB_INVALID_NUM);
        result = MMI_TRUE;
    }
    else if (lookUpNumber((S8*)ucs2_num))
    {
        MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_IS_NUM_IN_PHB_IN_PHB);
        result = MMI_TRUE;    
    }
    else if (mmi_phb_check_if_data_duplicated(number_UCS2, MMI_PHB_FIELDS_TYPE_NUMBER) == MMI_TRUE)
    {
        MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_IS_NUM_IN_PHB_IN_UNSAVED_LIST);
        result = MMI_TRUE;
    }
    else
    {
        MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_IS_NUM_IN_PHB_NOT_IN_PHB);
        result = MMI_FALSE;
    }
    return result;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_add_data_to_unsaved_list
 * DESCRIPTION
 *  save the data to the unsaved list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_add_data_to_unsaved_list(MMI_PHB_STORAGE_LOCATION storage, mmi_phb_fields_type_enum show_type, MMI_PHB_FIELDS_STRUCT *phb_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_phb_unsaved_data_node_struct* contact_node;
    mmi_phb_unsaved_data_node_struct* del_contact_node;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_ADD_DATA_TO_UNSAVED_LIST);
    ASSERT(g_phb_cntx.save_contact_notify == MMI_PHB_SAVE_CONTACT_NOTIFY_ON);
    if (storage == MMI_STORAGE_NONE || storage == MMI_STORAGE_BOTH)
    {
        storage = g_phb_cntx.prefer_storage;
    }
    contact_node = mmi_phb_unsaved_data_init_node(storage, show_type, phb_data);

    if (g_phb_unsaved_data_num < MMI_PHB_MAX_UNSAVED_DATA_NUM)
    {
        mmi_phb_unsaved_data_add_node(contact_node);
        g_phb_unsaved_data_num++;
    }
    else
    {
        del_contact_node = mmi_phb_unsaved_data_search_node(0);
        mmi_phb_unsaved_data_delete_node(del_contact_node);
        mmi_phb_unsaved_data_add_node(contact_node);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_populate_unsaved_data_to_list
 * DESCRIPTION
 *  populate data in node
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
U8* mmi_phb_populate_unsaved_data_to_list(U8 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_phb_unsaved_data_node_struct* contact_node;
    S8* data_ptr = NULL;
    U8 field_type;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    contact_node = mmi_phb_unsaved_data_search_node(index);
    field_type = contact_node->object->field_type;
    
    switch(field_type)
    {
        case MMI_PHB_FIELDS_TYPE_NAME:
            data_ptr = contact_node->object->phb_data->name;
            break;
        case MMI_PHB_FIELDS_TYPE_NUMBER:
            data_ptr = contact_node->object->phb_data->number;
            break;
        default:
            break;
    }
    return (PU8) data_ptr;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_storage_type
 * DESCRIPTION
 *  get storage type for an unsaved data node
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
U8 mmi_phb_unsaved_data_storage_type(U8 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_phb_unsaved_data_node_struct* contact_node;
    U8 storage;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    contact_node = mmi_phb_unsaved_data_search_node(index);
    storage = contact_node->object->storage;
    
    return storage;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_populate_unsaved_data_to_buff
 * DESCRIPTION
 *  populate unsaved data to phb global buffer
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_populate_unsaved_data_to_buff(U8 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_phb_unsaved_data_node_struct* contact_node;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    contact_node = mmi_phb_unsaved_data_search_node(index);

    if (contact_node->object->phb_data->name != NULL && mmi_ucs2strlen(contact_node->object->phb_data->name))
    {
        mmi_ucs2ncpy(pbName, contact_node->object->phb_data->name, MAX_PB_NAME_LENGTH);
    }
    
    if (contact_node->object->phb_data->number != NULL && mmi_ucs2strlen(contact_node->object->phb_data->number))
    {
        switch (g_phb_cntx.active_index_third)
        {
            case 0:
                mmi_ucs2ncpy(pbNumber, contact_node->object->phb_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
        #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
            case 1:
                mmi_ucs2ncpy(pbHomeNumber, contact_node->object->phb_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
            case 2:
                mmi_ucs2ncpy(pbOfficeNumber, contact_node->object->phb_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
            case 3:
                mmi_ucs2ncpy(pbFaxNumber, contact_node->object->phb_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
        #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
            default:
                mmi_ucs2ncpy(pbNumber, contact_node->object->phb_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
        }
    #if 0 /* Stage 1 begin */
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
        #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
        #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #endif /* Stage 1 end */
    }

#if defined(__MMI_PHB_INFO_FIELD__)
    if (contact_node->object->phb_data->pobox != NULL && mmi_ucs2strlen(contact_node->object->phb_data->pobox))
    {
        mmi_ucs2ncpy(pbAdrPOBox, contact_node->object->phb_data->pobox, MAX_PB_ADDRESS_LENGTH);
    }

    if (contact_node->object->phb_data->extension != NULL && mmi_ucs2strlen(contact_node->object->phb_data->extension))
    {
        mmi_ucs2ncpy(pbAdrExtension, contact_node->object->phb_data->extension, MAX_PB_ADDRESS_LENGTH);
    }

    if (contact_node->object->phb_data->street != NULL && mmi_ucs2strlen(contact_node->object->phb_data->street))
    {
        mmi_ucs2ncpy(pbAdrStreet, contact_node->object->phb_data->street, MAX_PB_ADDRESS_LENGTH);
    }

    if (contact_node->object->phb_data->city != NULL && mmi_ucs2strlen(contact_node->object->phb_data->city))
    {
        mmi_ucs2ncpy(pbAdrCity, contact_node->object->phb_data->city, MAX_PB_ADDRESS_LENGTH);
    }

    if (contact_node->object->phb_data->state != NULL && mmi_ucs2strlen(contact_node->object->phb_data->state))
    {
        mmi_ucs2ncpy(pbAdrState, contact_node->object->phb_data->state, MAX_PB_ADDRESS_LENGTH);
    }

    if (contact_node->object->phb_data->postalcode != NULL && mmi_ucs2strlen(contact_node->object->phb_data->postalcode))
    {
        mmi_ucs2ncpy(pbAdrPostalCode, contact_node->object->phb_data->postalcode, MAX_PB_ADDRESS_LENGTH);
    }

    if (contact_node->object->phb_data->country != NULL && mmi_ucs2strlen(contact_node->object->phb_data->country))
    {
        mmi_ucs2ncpy(pbAdrCountry, contact_node->object->phb_data->country, MAX_PB_ADDRESS_LENGTH);
    }
#endif /* __MMI_PHB_INFO_FIELD__ */
    /* remember to set pbname, ... */
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_delete_unsaved_data
 * DESCRIPTION
 *  delete ith node and related screen after add new or replace a contact
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_delete_unsaved_data(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_phb_unsaved_data_node_struct* contact_node;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(g_phb_unsaved_data_index < g_phb_unsaved_data_num);

    contact_node = mmi_phb_unsaved_data_search_node(g_phb_unsaved_data_index);
    mmi_phb_unsaved_data_delete_node(contact_node);
    g_phb_unsaved_data_num--;
    DeleteScreenIfPresent(SCR_ID_PHB_UPDATE_DATA_LIST);
    DeleteScreenIfPresent(SCR_ID_PHB_UNSAVED_DATA_OPTIONS);
    if (g_phb_unsaved_data_num == 0)
    {
        DeleteScreenIfPresent(SCR_ID_PHB_UNSAVED_DATA_LIST);
        DeleteScreenIfPresent(SCR_ID_PHB_UNSAVED_DATA_CONFIRM);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_confirm_delete_cb
 * DESCRIPTION
 *  Delete callback of mmi_phb_entry_unsaved_data_confirm
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
U8 mmi_phb_unsaved_data_confirm_delete_cb(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (g_phb_unsaved_data_op_state == MMI_PHB_UNSAVED_DATA_OP_NORMAL)
    {
        mmi_phb_unsaved_data_delete_list();
        g_phb_unsaved_data_num = 0;
    }
    if (g_phb_enter_from == MMI_PHB_ENTER_FROM_UNSAVED_DATA)
    {
        g_phb_enter_from = MMI_PHB_ENTER_NONE;
    }
    return MMI_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_exit_unsaved_data_confirm
 * DESCRIPTION
 *  exit function for unsaved_data_confirm
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_exit_unsaved_data_confirm(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    //stopRequestedTone(WARNING_TONE);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_unsaved_data_confirm
 * DESCRIPTION
 *  entry function for unsaved_data_confirm
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_unsaved_data_confirm(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S8 *outBuf;
    S8 count[10];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_ENTRY_UNSAVED_DATA_CONFIRM, g_phb_unsaved_data_num);
    ASSERT(g_phb_unsaved_data_num > 0);
    TurnOnBacklight(GPIO_BACKLIGHT_SHORT_TIME);
    EntryNewScreen(SCR_ID_PHB_UNSAVED_DATA_CONFIRM, mmi_phb_exit_unsaved_data_confirm, mmi_phb_entry_unsaved_data_confirm, NULL);
    SetDelScrnIDCallbackHandler(SCR_ID_PHB_UNSAVED_DATA_CONFIRM, (HistoryDelCBPtr)mmi_phb_unsaved_data_confirm_delete_cb);
    g_phb_unsaved_data_op_state = MMI_PHB_UNSAVED_DATA_OP_NORMAL;

    outBuf = OslMalloc(256);
    memset(outBuf, 0x00, 256);

    if (g_phb_unsaved_data_num == 1)
    {
        g_phb_unsaved_data_index = 0;
        mmi_ucs2cpy(outBuf, GetString(STR_ID_SAVE_CONTACT_SAVE));
        mmi_ucs2cat(outBuf, (S8*) mmi_phb_populate_unsaved_data_to_list(g_phb_unsaved_data_index));
        mmi_ucs2cat(outBuf, GetString(STR_ID_SAVE_CONTACT_TO_PHB));
        ShowCategory66Screen(
            STR_SCR_PBOOK_CAPTION,
            IMG_SCR_PBOOK_CAPTION,
            STR_GLOBAL_YES,
            IMG_GLOBAL_YES,
            STR_GLOBAL_NO,
            IMG_GLOBAL_NO,
            (PU8) outBuf,
            IMG_GLOBAL_QUESTION,
            NULL);
        SetLeftSoftkeyFunction(mmi_phb_entry_save_contact_option, KEY_EVENT_UP);
    }
    else
    {
        sprintf(outBuf, "%d ", g_phb_unsaved_data_num);
        mmi_asc_to_ucs2(count, outBuf);
        mmi_ucs2cpy(outBuf, GetString(STR_ID_SAVE_CONTACT_SAVE));
        mmi_ucs2cat(outBuf, count);
        mmi_ucs2cat(outBuf, GetString(STR_ID_SAVE_CONTACT_UNSAVED_DATA));

        ShowCategory66Screen(
            STR_SCR_PBOOK_CAPTION,
            IMG_SCR_PBOOK_CAPTION,
            STR_GLOBAL_YES,
            IMG_GLOBAL_YES,
            STR_GLOBAL_NO,
            IMG_GLOBAL_NO,
            (PU8) outBuf,
            IMG_GLOBAL_QUESTION,
            NULL);
        SetLeftSoftkeyFunction(mmi_phb_entry_unsaved_contact_list, KEY_EVENT_UP);
    }
    OslMfree(outBuf);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
    if (g_phb_is_first_time_confirm == MMI_TRUE)
    {
        //playRequestedTone(WARNING_TONE);
        g_phb_is_first_time_confirm = MMI_FALSE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_check_if_scr_older
 * DESCRIPTION
 *  check if check_scr older than base_scr
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static MMI_BOOL mmi_phb_check_if_scr_older(U16 check_scr, U16 base_scr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 cur_scr = base_scr;
    U16 pre_scr = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!IsScreenPresent(check_scr))
    {
        return MMI_FALSE;
    }

    if (!IsScreenPresent(base_scr))
    {
        return MMI_FALSE;
    }

    while (GetPreviousScrnIdOf(cur_scr, &pre_scr))
    {
        if (pre_scr == check_scr)
        {
            return MMI_TRUE;
        }

        cur_scr = pre_scr;
        pre_scr = 0;
    }

    return MMI_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_reentry_unsaved_data_clear_history
 * DESCRIPTION
 *  clear the history data for save contact notify related screen
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_reentry_unsaved_data_clear_history(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 next_id;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while(GetNextScrnIdOf(SCR_ID_PHB_UNSAVED_DATA_CONFIRM, &next_id))
    {
        if (next_id > SCR_PBOOK_MAIN_MENU && next_id < SCR_ID_PHB_ENUM_TOTAL)
        {
            if (next_id == SCR_ADD_NEW_ENTRY)
            {
                mmi_phb_clear_old_add_history();
            }
            
            DeleteScreenIfPresent(next_id);
        }
        else
        {
            break;
        }
    }
    
    DeleteScreenIfPresent(SCR_ID_PHB_UNSAVED_DATA_CONFIRM);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_pre_entry_unsaved_data_confirm
 * DESCRIPTION
 *  pre_entry function for unsaved_data_confirm
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_pre_entry_unsaved_data_confirm(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_PRE_ENTRY_UNSAVED_DATA_CONFIRM);
    ASSERT(g_phb_cntx.save_contact_notify == MMI_PHB_SAVE_CONTACT_NOTIFY_ON);
    if (IsScreenPresent(SCR_ID_PHB_UNSAVED_DATA_CONFIRM) == MMI_TRUE)
    {
        g_phb_unsaved_data_op_state = MMI_PHB_UNSAVED_DATA_OP_REENTRY;
    }
    mmi_phb_reentry_unsaved_data_clear_history();
    g_phb_is_first_time_confirm = MMI_TRUE;
    mmi_phb_entry_unsaved_data_confirm();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_add_unsaved_data_confirm_to_history
 * DESCRIPTION
 *  add unsaved
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_add_unsaved_data_confirm_to_history(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_frm_screen_struct inserted_scrn;
    U16 scr_id = MMI_HIST_TOP_SCREEN;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_ADD_UNSAVED_DATA_CONFIRM_TO_HISTORY);
    ASSERT(g_phb_cntx.save_contact_notify == MMI_PHB_SAVE_CONTACT_NOTIFY_ON);
    
    if (IsScreenPresent(SCR_ID_PHB_UNSAVED_DATA_CONFIRM) == MMI_TRUE)
    {
        g_phb_unsaved_data_op_state = MMI_PHB_UNSAVED_DATA_OP_REENTRY;
    }
    else if (GetActiveScreenId() == SCR_ID_PHB_UNSAVED_DATA_CONFIRM)
    {
        g_phb_unsaved_data_op_state = MMI_PHB_UNSAVED_DATA_OP_REENTRY;
        EntryNewScreen(SCR_PHB_DUMMY, NULL, NULL, 0);
        DeleteScreenIfPresent(SCR_ID_PHB_UNSAVED_DATA_CONFIRM);
        mmi_phb_entry_unsaved_data_confirm();
    
        return;
    }
	
    mmi_phb_reentry_unsaved_data_clear_history();

    mmi_frm_init_screen_struct(&inserted_scrn);
    inserted_scrn.scrnID = SCR_ID_PHB_UNSAVED_DATA_CONFIRM;
#ifdef __MMI_UI_SMALL_SCREEN_SUPPORT__
    inserted_scrn.isSmallScreen = 0;
#endif 
    inserted_scrn.entryFuncPtr = (entry_func_ptr)mmi_phb_entry_unsaved_data_confirm;
    if (isIncomingCall())
    {
        scr_id = GetCmMarkerScrnID();
    }

    mmi_frm_insert_screen(scr_id, &inserted_scrn, MMI_FRM_INSERT_AFTER_SCRN);
    SetDelScrnIDCallbackHandler(SCR_ID_PHB_UNSAVED_DATA_CONFIRM, (HistoryDelCBPtr)mmi_phb_unsaved_data_confirm_delete_cb);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_insert_unsaved_data_confirm_after_assigned_scrn
 * DESCRIPTION
 *  add unsaved
 * PARAMETERS
 *  scrn_id
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_insert_unsaved_data_confirm_after_assigned_scrn(U16 scrn_id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    historyNode *ptrHistory;
    history hist;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(g_phb_cntx.save_contact_notify == MMI_PHB_SAVE_CONTACT_NOTIFY_ON);
    if (IsScreenPresent(SCR_ID_PHB_UNSAVED_DATA_CONFIRM) == MMI_TRUE)
    {
        g_phb_unsaved_data_op_state = MMI_PHB_UNSAVED_DATA_OP_REENTRY;
    }
    mmi_phb_reentry_unsaved_data_clear_history();
    g_phb_is_first_time_confirm = MMI_TRUE;
    
    hist.scrnID = SCR_ID_PHB_UNSAVED_DATA_CONFIRM;
    hist.entryFuncPtr = mmi_phb_entry_unsaved_data_confirm;
    memset(hist.guiBuffer, 0, MAX_GUI_BUFFER);
    
    SetDelScrnIDCallbackHandler(SCR_ID_PHB_UNSAVED_DATA_CONFIRM, (HistoryDelCBPtr)mmi_phb_unsaved_data_confirm_delete_cb);                    
    ASSERT(InsertHistoryAfterThisScrnReference(scrn_id, &hist) == MMI_TRUE);
	
#ifdef __MMI_UI_SMALL_SCREEN_SUPPORT__
    GetHistoryPointer(SCR_ID_PHB_UNSAVED_DATA_CONFIRM, &ptrHistory);
    ptrHistory->isSmallScreen = 0;
#endif 
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_get_unsaved_data_num
 * DESCRIPTION
 *  get unsaved data num
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
U8 mmi_phb_get_unsaved_data_num(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return g_phb_unsaved_data_num;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_go_back_history_for_save_contact_option
 * DESCRIPTION
 *  go back history function for save contact option screen
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_go_back_history_for_save_contact_option(void)
{
    if (!IsScreenPresent(SCR_ID_PHB_UNSAVED_DATA_LIST))
    {
        DeleteScreenIfPresent(SCR_ID_PHB_UNSAVED_DATA_CONFIRM);
    }
    GoBackHistory();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_save_contact_option
 * DESCRIPTION
 *  entry screen of save contact option
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_save_contact_option(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 nStrItemList[MAX_SUB_MENUS];
    U16 nNumofItem;
    U8 *guiBuffer;
    U8 storage_type;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_ENTRY_SAVE_CONTACT_OPTION);
    ASSERT(g_phb_unsaved_data_index < g_phb_unsaved_data_num);

    EntryNewScreen(SCR_ID_PHB_UNSAVED_DATA_OPTIONS, NULL, mmi_phb_entry_save_contact_option, NULL);

    storage_type = mmi_phb_unsaved_data_storage_type(g_phb_unsaved_data_index);

    mmi_frm_unhide_menu_item(MENU_ID_SAVE_CONTACT_NOTIFY_OPT_ADD);
    mmi_frm_unhide_menu_item(MENU_ID_SAVE_CONTACT_NOTIFY_OPT_UPDATE);

    if (mmi_phb_num_of_phb_contact_in_storage(storage_type) == 0)
    {
        mmi_frm_hide_menu_item(MENU_ID_SAVE_CONTACT_NOTIFY_OPT_UPDATE);
    }
    if (mmi_phb_num_of_phb_contact_in_storage(storage_type) == mmi_phb_capacity_of_phb_contact_in_storage(storage_type))
    {
        mmi_frm_hide_menu_item(MENU_ID_SAVE_CONTACT_NOTIFY_OPT_ADD);
    }
    
    guiBuffer = GetCurrGuiBuffer(SCR_ID_PHB_UNSAVED_DATA_OPTIONS);
    nNumofItem = GetNumOfChild_Ext(MENU_ID_SAVE_CONTACT_NOTIFY_OPT);
    GetSequenceStringIds_Ext(MENU_ID_SAVE_CONTACT_NOTIFY_OPT, nStrItemList);
    SetParentHandler(MENU_ID_SAVE_CONTACT_NOTIFY_OPT);
    RegisterHighlightHandler(ExecuteCurrHiliteHandler);

    ShowCategory15Screen(
        STR_GLOBAL_OPTIONS,
        GetRootTitleIcon(MAIN_MENU_PHONEBOOK_MENUID),
        STR_GLOBAL_OK,
        IMG_GLOBAL_OK,
        STR_GLOBAL_BACK,
        IMG_GLOBAL_BACK,
        nNumofItem,
        nStrItemList,
        (U16*) gIndexIconsImageList,
        1,
        0,
        guiBuffer);
    if (IsScreenPresent(SCR_ID_PHB_UNSAVED_DATA_LIST))
    {
        SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
        SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);
    }
    else
    {
        SetRightSoftkeyFunction(mmi_phb_go_back_history_for_save_contact_option, KEY_EVENT_UP);
        ClearKeyHandler(KEY_LEFT_ARROW, KEY_EVENT_DOWN);
    }
    return;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_get_item
 * DESCRIPTION
 *  get item function for unsaved data list screen
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
pBOOL mmi_phb_unsaved_data_get_item(S32 item_index, UI_string_type str_buff, PU8 *img_buff_p, U8 str_img_mask)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8* data_buff;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    data_buff = mmi_phb_populate_unsaved_data_to_list((U8)item_index);
    mmi_ucs2cpy((S8*) str_buff, (S8*) data_buff);
    *img_buff_p = get_image((U16)(IMG_GLOBAL_L1 + item_index));
    str_img_mask = 0;
    return TRUE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_list_hilite_handler
 * DESCRIPTION
 *  highlight handler for unsaved data list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_unsaved_data_list_hilite_handler(S32 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_unsaved_data_index = index;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_go_back_history_for_unsaved_contact_list
 * DESCRIPTION
 *  go back histroy function for unsaved contact list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_go_back_history_for_unsaved_contact_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    DeleteScreenIfPresent(SCR_ID_PHB_UNSAVED_DATA_CONFIRM);
    GoBackHistory();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_unsaved_contact_list
 * DESCRIPTION
 *  entry function for unsaved contact list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_unsaved_contact_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 *guiBuffer;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_ENTRY_UNSAVED_CONTACT_LIST);
    EntryNewScreen(SCR_ID_PHB_UNSAVED_DATA_LIST, NULL, mmi_phb_entry_unsaved_contact_list, NULL);

    guiBuffer = GetCurrGuiBuffer(SCR_ID_PHB_UNSAVED_DATA_LIST);
    RegisterHighlightHandler(mmi_phb_unsaved_data_list_hilite_handler);

    if (guiBuffer == NULL)
    {
        g_phb_cntx.highlight_entry = 0;
    }
    ShowCategory184Screen(
        STR_ID_SAVE_CONTACT_NEW_CONTACT_LIST,
        IMG_SCR_PBOOK_CAPTION,
        STR_GLOBAL_OPTIONS,
        IMG_GLOBAL_OPTIONS,
        STR_GLOBAL_BACK,
        IMG_GLOBAL_BACK,
        g_phb_unsaved_data_num,
        mmi_phb_unsaved_data_get_item,
        NULL,
        g_phb_cntx.highlight_entry,
        guiBuffer);

    SetLeftSoftkeyFunction(mmi_phb_entry_save_contact_option, KEY_EVENT_UP);
    SetRightSoftkeyFunction(mmi_phb_go_back_history_for_unsaved_contact_list, KEY_EVENT_UP);
    ClearKeyHandler(KEY_LEFT_ARROW, KEY_EVENT_DOWN);

    return;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_highlight_unsaved_data_opt_add
 * DESCRIPTION
 *  highlight function for unsaved data option add
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_highlight_unsaved_data_opt_add(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SetLeftSoftkeyFunction(mmi_phb_unsaved_data_opt_add, KEY_EVENT_UP);
    SetKeyHandler(mmi_phb_unsaved_data_opt_add, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_highlight_unsaved_data_opt_update
 * DESCRIPTION
 *  highlight function for unsaved data option update
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_highlight_unsaved_data_opt_update(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SetLeftSoftkeyFunction(mmi_phb_unsaved_data_opt_update, KEY_EVENT_UP);
    SetKeyHandler(mmi_phb_unsaved_data_opt_update, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_opt_add
 * DESCRIPTION
 *  add new contact for unsaved data
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_unsaved_data_opt_add(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 storage;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_DUAL_SIM_MASTER__
    if (MTPNP_PFAL_Phb_IsReady() == MTPNP_FALSE)
#else   /* __MMI_DUAL_SIM_MASTER__ */
    if (!g_phb_cntx.phb_ready || g_phb_cntx.processing)
#endif  /* __MMI_DUAL_SIM_MASTER__ */
    {
        mmi_phb_entry_not_ready(STR_PROCESSING_PHONEBOOK);
    }
#ifdef __MMI_PHB_USIM_FIELD__
    else if (g_phb_cntx.is_usim && g_phb_cntx.usim_ready_stage != MMI_PHB_USIM_READY)
    {
        mmi_phb_entry_not_ready(STR_ID_PHB_PROCESSING_USIM);
    }
#endif /* __MMI_PHB_USIM_FIELD__ */
#ifdef __SYNCML_SUPPORT__
    else if (mmi_syncml_is_phb_sync_now())
    {
        mmi_phb_entry_not_ready(STR_ID_SYNC_PLEASE_WAIT);
    }
#endif /* __SYNCML_SUPPORT__ */
#ifdef __MMI_DUAL_SIM_MASTER__
    else if (PhoneBookEntryCount == g_phb_cntx.sim_total + g_phb_cntx.phone_total + MTPNP_AD_ADN_SIM2GetCapacity())
#else   /* __MMI_DUAL_SIM_MASTER__ */
    else if (PhoneBookEntryCount == (g_phb_cntx.sim_total + g_phb_cntx.phone_total))
#endif  /* __MMI_DUAL_SIM_MASTER__ */
    {
        mmi_display_popup((UI_string_type)GetString(STR_PBOOK_FULL_MSG), MMI_EVENT_FAILURE);
    }
    else
    {
        g_phb_cntx.set_done_flag = 1;
        mmi_phb_clear_old_history();
        mmi_phb_util_clear_buffer(TRUE);
        g_phb_cntx.active_index_third = 0;
        g_phb_enter_from = MMI_PHB_ENTER_FROM_UNSAVED_DATA;
        mmi_phb_populate_unsaved_data_to_buff(g_phb_unsaved_data_index);
        storage = mmi_phb_unsaved_data_storage_type(g_phb_unsaved_data_index);
        if (storage == MMI_STORAGE_BOTH)
        {
            mmi_phb_entry_op_add_choose_storage();
        }
        else
        {
            g_phb_cntx.selected_storage = storage;
        #ifdef __MMI_PHB_SIM_NUMBER_MANAGEMENT__
            g_phb_add_more_op = MMI_PHB_OP_NONE;
        #endif
            mmi_phb_entry_op_add_entry();
        }
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_update_contact
 * DESCRIPTION
 *  replace contact for unsaved data
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_unsaved_data_update_contact(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index;
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)    
    S16 pError;
#endif
    U8 storage_type;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    storage_type = mmi_phb_unsaved_data_storage_type(g_phb_unsaved_data_index);
    store_index = g_phb_name_index[mmi_phb_nlf_get_sorted_index(g_phb_cntx.active_index, storage_type)];

#ifdef __MMI_DUAL_SIM_MASTER__
    if ((store_index >= MAX_PB_PHONE_ENTRIES) && (store_index < (MAX_PB_PHONE_ENTRIES + MAX_PB_SIM_ENTRIES)))
    {
        g_phb_cntx.selected_storage = MMI_SIM;
    }
    else if(store_index < MAX_PB_PHONE_ENTRIES)
    {
        g_phb_cntx.selected_storage = MMI_NVRAM;
    }
    else if (store_index >= (MAX_PB_PHONE_ENTRIES+MAX_PB_SIM_ENTRIES))
    {
        g_phb_cntx.selected_storage = MMI_SIM2;
    }
#else /* __MMI_DUAL_SIM_MASTER__ */
    if (store_index >= MAX_PB_PHONE_ENTRIES)
    {
        g_phb_cntx.selected_storage = MMI_SIM;
    }
    else if (store_index < MAX_PB_PHONE_ENTRIES)
    {
        g_phb_cntx.selected_storage = MMI_NVRAM;
    }
#endif /* __MMI_DUAL_SIM_MASTER__ */

    mmi_phb_util_clear_buffer(TRUE);

    mmi_ucs2cpy(pbName, (PS8) PhoneBook[store_index].alpha_id.name);
    mmi_phb_convert_get_ucs2_number(pbNumber, store_index);
    if (store_index < MAX_PB_PHONE_ENTRIES)
    {
    #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
        ReadRecord(
            NVRAM_EF_PHB_FIELDS_LID,
            (U16) (store_index + 1),
            (void*)&PhoneBookOptionalFields,
            OPTIONAL_FIELDS_RECORD_SIZE,
            &pError);

        mmi_asc_to_ucs2(pbHomeNumber, (PS8) PhoneBookOptionalFields.homeNumber);
        mmi_ucs2cpy(pbCompanyName, (PS8) PhoneBookOptionalFields.companyName);
        mmi_asc_to_ucs2(pbEmailAddress, (PS8) PhoneBookOptionalFields.emailAddress);
        mmi_asc_to_ucs2(pbOfficeNumber, (PS8) PhoneBookOptionalFields.officeNumber);
        mmi_asc_to_ucs2(pbFaxNumber, (PS8) PhoneBookOptionalFields.faxNumber);
    #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
#if defined(__MMI_PHB_DEFAULT_NUMBER__)
    g_phb_assigned_default_number = mmi_phb_og_default_number_id(store_index);
#endif /* __MMI_PHB_DEFAULT_NUMBER__ */

    #if defined(__MMI_PHB_BIRTHDAY_FIELD__)
        mmi_phb_bday_read_data_to_buff(store_index);
    #endif

    #if defined(__MMI_IMPS__) || defined(__MMI_PHB_POC_FIELD__) || defined(__MMI_VOIP__)
        mmi_phb_service_read_data_to_buff(store_index);
    #endif /* defined(__MMI_IMPS__) || defined(__MMI_PHB_POC_FIELD__) || defined(__MMI_VOIP__) */
    
    #if defined(__MMI_PHB_INFO_FIELD__)
        mmi_phb_info_read_data_to_buff(store_index);
    #endif /* defined(__MMI_PHB_INFO_FIELD__) */
    }

    mmi_phb_populate_unsaved_data_to_buff(g_phb_unsaved_data_index);
    if (!mmi_phb_op_check_pre_save(TRUE))
    {
        return;
    }
    g_phb_cntx.active_index = mmi_phb_nlf_get_sorted_index(g_phb_cntx.active_index, storage_type);
    g_phb_enter_from = MMI_PHB_ENTER_FROM_UNSAVED_DATA;
    mmi_phb_op_edit_entry_req();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_unsaved_data_update_contact_confirm
 * DESCRIPTION
 *  update contact for unsaved data
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_unsaved_data_update_contact_confirm(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_ENTRY_UNSAVED_DATA_UPDATE_CONTACT_CONFIRM);

    mmi_display_popup_confirm(
        (UI_string_type) GetString(STR_GLOBAL_YES),
        (U8*) GetImage(IMG_GLOBAL_YES), 
        (UI_string_type) GetString(STR_GLOBAL_NO),
        (U8*) GetImage(IMG_GLOBAL_NO), 
        (UI_string_type) GetString(STR_ID_SAVE_CONTACT_REPLACE_QUERY),
        MMI_EVENT_QUERY);

    SetLeftSoftkeyFunction(mmi_phb_unsaved_data_update_contact, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_unsaved_data_opt_select_field
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_unsaved_data_opt_select_field(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 storage_type;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    storage_type = mmi_phb_unsaved_data_storage_type(g_phb_unsaved_data_index);
    mmi_phb_entry_choose_field(
        g_phb_name_index[mmi_phb_nlf_get_sorted_index(g_phb_cntx.active_index, storage_type)],
        MMI_PHB_ENTRY_FIELD_GSM_NUM,
        mmi_phb_entry_unsaved_data_opt_select_field,
        mmi_phb_entry_unsaved_data_update_contact_confirm,
        GoBackHistory,
        NULL,
        MMI_TRUE);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_exit_update_list
 * DESCRIPTION
 *  exit function for update list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_exit_update_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.end_scr_id = SCR_ID_PHB_UPDATE_DATA_LIST;
    wgui_reset_multitap();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_update_list
 * DESCRIPTION
 *  entry function for update list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_update_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 *guiBuffer;
    U8 storage_type;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_DUAL_SIM_MASTER__
    if (MTPNP_PFAL_Phb_IsReady() == MTPNP_FALSE)
#else   /* __MMI_DUAL_SIM_MASTER__ */
    if (!g_phb_cntx.phb_ready || g_phb_cntx.processing)
#endif  /* __MMI_DUAL_SIM_MASTER__ */
    {
        mmi_phb_entry_not_ready(STR_PROCESSING_PHONEBOOK);
    }
#ifdef __MMI_PHB_USIM_FIELD__
    else if (g_phb_cntx.is_usim && g_phb_cntx.usim_ready_stage != MMI_PHB_USIM_READY)
    {
        mmi_phb_entry_not_ready(STR_ID_PHB_PROCESSING_USIM);
    }
#endif /* __MMI_PHB_USIM_FIELD__ */
#ifdef __SYNCML_SUPPORT__
    else if (mmi_syncml_is_phb_sync_now())
    {
        mmi_phb_entry_not_ready(STR_ID_SYNC_PLEASE_WAIT);
    }
#endif /* __SYNCML_SUPPORT__ */
    else if (mmi_phb_num_of_phb_contact_in_storage(mmi_phb_unsaved_data_storage_type(g_phb_unsaved_data_index)) == 0)
    {
        mmi_display_popup((UI_string_type)GetString(STR_NO_ENTRIES_MESSAGE), MMI_EVENT_FAILURE);
        return;
    }
    else
    {
        MMI_TRACE(MMI_PHB_TRACE_GROUP, TRC_MMI_PHB_ENTRY_UPDATE_LIST);
        EntryNewScreen(SCR_ID_PHB_UPDATE_DATA_LIST, mmi_phb_exit_update_list, mmi_phb_entry_update_list, MMI_FULL_SCREEN);
        guiBuffer = GetCurrGuiBuffer(SCR_ID_PHB_UPDATE_DATA_LIST);

        storage_type = mmi_phb_unsaved_data_storage_type(g_phb_unsaved_data_index);
        /* bulid alpha_index function. */
        mmi_phb_list_build_alpha_index(NULL, 0, TRUE, storage_type);

        if (guiBuffer == NULL)
        {
            g_phb_cntx.highlight_entry = 0;
        }

        RegisterHighlightHandler(mmi_phb_get_index);
        g_phb_nlf_list_storage = storage_type;

        ShowCategory184Screen(
            STR_SCR_PBOOK_VIEW_CAPTION,
            IMG_SCR_PBOOK_CAPTION,
            STR_GLOBAL_OK,
            IMG_GLOBAL_OK,
            STR_GLOBAL_BACK,
            IMG_GLOBAL_BACK,
            mmi_phb_num_of_phb_contact_in_storage(storage_type),
            mmi_phb_nlf_list_get_item,
            mmi_phb_nlf_list_get_hint,
            g_phb_cntx.highlight_entry,
            guiBuffer);

        SetLeftSoftkeyFunction(mmi_phb_entry_unsaved_data_opt_select_field, KEY_EVENT_UP);
        SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
        
        /* register multitap key for fast jump by alpha index */
#ifdef __MMI_QWERTY_KEYPAD_SUPPORT__
        mmi_frm_set_keypad_mode(KEYPAD_1KEY_QWERTY);
        mmi_frm_set_app_translate_key_handler(mmi_phb_list_alpha_callback);
#else
        wgui_setup_multitap_functions(WGUI_MULTITAP_TYPE_UPPERCASE_ABC, mmi_phb_list_alpha_index, mmi_phb_list_alpha_index_input_callback);
#endif

        //SetKeyHandler(mmi_phb_entry_unsaved_data_update_contact_confirm, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
        SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_opt_update
 * DESCRIPTION
 *  handler for update existed contact
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_unsaved_data_opt_update(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_clear_old_history();
    mmi_phb_entry_update_list();
}
#endif /* __MMI_PHB_SAVE_CONTACT_NOTIFY__ */


/* general save contact option start */
/*****************************************************************************
 * FUNCTION
 *  mmi_phb_populate_save_contact_data_to_buff
 * DESCRIPTION
 *  populate unsaved data to phb global buffer
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_populate_save_contact_data_to_buff(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(g_phb_save_contact_data != NULL);
    if (g_phb_save_contact_data->name != NULL && mmi_ucs2strlen(g_phb_save_contact_data->name))
    {
        mmi_ucs2ncpy(pbName, g_phb_save_contact_data->name, MAX_PB_NAME_LENGTH);
    }
    
    if (g_phb_save_contact_data->number != NULL && mmi_ucs2strlen(g_phb_save_contact_data->number))
    {
        switch (g_phb_cntx.active_index_third)
        {
            case 0:
                mmi_ucs2ncpy(pbNumber, g_phb_save_contact_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
        #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
            case 1:
                mmi_ucs2ncpy(pbHomeNumber, g_phb_save_contact_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
            case 2:
                mmi_ucs2ncpy(pbOfficeNumber, g_phb_save_contact_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
            case 3:
                mmi_ucs2ncpy(pbFaxNumber, g_phb_save_contact_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
        #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
            default:
                mmi_ucs2ncpy(pbNumber, g_phb_save_contact_data->number, MAX_PB_NUMBER_LENGTH + 1);
                break;
        }
    #if 0 /* Stage 1 begin */
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
        #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
        #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #endif /* Stage 1 end */
    }

#if defined(__MMI_PHB_INFO_FIELD__)
    if (g_phb_save_contact_data->pobox != NULL && mmi_ucs2strlen(g_phb_save_contact_data->pobox))
    {
        mmi_ucs2ncpy(pbAdrPOBox, g_phb_save_contact_data->pobox, MAX_PB_ADDRESS_LENGTH);
    }

    if (g_phb_save_contact_data->extension != NULL && mmi_ucs2strlen(g_phb_save_contact_data->extension))
    {
        mmi_ucs2ncpy(pbAdrExtension, g_phb_save_contact_data->extension, MAX_PB_ADDRESS_LENGTH);
    }

    if (g_phb_save_contact_data->street != NULL && mmi_ucs2strlen(g_phb_save_contact_data->street))
    {
        mmi_ucs2ncpy(pbAdrStreet, g_phb_save_contact_data->street, MAX_PB_ADDRESS_LENGTH);
    }

    if (g_phb_save_contact_data->city != NULL && mmi_ucs2strlen(g_phb_save_contact_data->city))
    {
        mmi_ucs2ncpy(pbAdrCity, g_phb_save_contact_data->city, MAX_PB_ADDRESS_LENGTH);
    }

    if (g_phb_save_contact_data->state != NULL && mmi_ucs2strlen(g_phb_save_contact_data->state))
    {
        mmi_ucs2ncpy(pbAdrState, g_phb_save_contact_data->state, MAX_PB_ADDRESS_LENGTH);
    }

    if (g_phb_save_contact_data->postalcode != NULL && mmi_ucs2strlen(g_phb_save_contact_data->postalcode))
    {
        mmi_ucs2ncpy(pbAdrPostalCode, g_phb_save_contact_data->postalcode, MAX_PB_ADDRESS_LENGTH);
    }

    if (g_phb_save_contact_data->country != NULL && mmi_ucs2strlen(g_phb_save_contact_data->country))
    {
        mmi_ucs2ncpy(pbAdrCountry, g_phb_save_contact_data->country, MAX_PB_ADDRESS_LENGTH);
    }
#endif /* __MMI_PHB_INFO_FIELD__ */
    /* remember to set pbname, ... */
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_save_contact_opt_add
 * DESCRIPTION
 *  save contact opt add to enter add new contact interface
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_save_contact_opt_add(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.set_done_flag = 1;
    mmi_phb_clear_old_history();
    mmi_phb_util_clear_buffer(TRUE);
    g_phb_cntx.active_index_third = 0;
    mmi_phb_populate_save_contact_data_to_buff();

    if (g_phb_save_contact_opt_storage == MMI_STORAGE_BOTH || g_phb_save_contact_opt_storage == MMI_STORAGE_NONE)
    {
        mmi_phb_entry_op_add_choose_storage();
    }
    else
    {
        g_phb_cntx.selected_storage = g_phb_save_contact_opt_storage;
    #ifdef __MMI_PHB_SIM_NUMBER_MANAGEMENT__
        g_phb_add_more_op = MMI_PHB_OP_NONE;
    #endif
        mmi_phb_entry_op_add_entry();
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_unsaved_data_update_contact
 * DESCRIPTION
 *  replace contact for unsaved data
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_save_contact_opt_update_contact(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 store_index;
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)    
    S16 pError;
#endif

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    store_index = g_phb_name_index[mmi_phb_nlf_get_sorted_index(g_phb_cntx.active_index, g_phb_save_contact_opt_storage)];

#ifdef __MMI_DUAL_SIM_MASTER__
    if ((store_index >= MAX_PB_PHONE_ENTRIES) && (store_index < (MAX_PB_PHONE_ENTRIES + MAX_PB_SIM_ENTRIES)))
    {
        g_phb_cntx.selected_storage = MMI_SIM;
    }
    else if(store_index < MAX_PB_PHONE_ENTRIES)
    {
        g_phb_cntx.selected_storage = MMI_NVRAM;
    }
    else if (store_index >= (MAX_PB_PHONE_ENTRIES + MAX_PB_SIM_ENTRIES))
    {
        g_phb_cntx.selected_storage = MMI_SIM2;
    }
#else /* __MMI_DUAL_SIM_MASTER__ */
    if (store_index >= MAX_PB_PHONE_ENTRIES)
    {
        g_phb_cntx.selected_storage = MMI_SIM;
    }
    else if (store_index < MAX_PB_PHONE_ENTRIES)
    {
        g_phb_cntx.selected_storage = MMI_NVRAM;
    }
#endif /* __MMI_DUAL_SIM_MASTER__ */

    mmi_phb_util_clear_buffer(TRUE);

    mmi_ucs2cpy(pbName, (PS8) PhoneBook[store_index].alpha_id.name);
    mmi_phb_convert_get_ucs2_number(pbNumber, store_index);
    if (store_index < MAX_PB_PHONE_ENTRIES)
    {
    #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
        ReadRecord(
            NVRAM_EF_PHB_FIELDS_LID,
            (U16) (store_index + 1),
            (void*)&PhoneBookOptionalFields,
            OPTIONAL_FIELDS_RECORD_SIZE,
            &pError);

        mmi_asc_to_ucs2(pbHomeNumber, (PS8) PhoneBookOptionalFields.homeNumber);
        mmi_ucs2cpy(pbCompanyName, (PS8) PhoneBookOptionalFields.companyName);
        mmi_asc_to_ucs2(pbEmailAddress, (PS8) PhoneBookOptionalFields.emailAddress);
        mmi_asc_to_ucs2(pbOfficeNumber, (PS8) PhoneBookOptionalFields.officeNumber);
        mmi_asc_to_ucs2(pbFaxNumber, (PS8) PhoneBookOptionalFields.faxNumber);
    #endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) */ 
#if defined(__MMI_PHB_DEFAULT_NUMBER__)
    g_phb_assigned_default_number = mmi_phb_og_default_number_id(store_index);
#endif /* __MMI_PHB_DEFAULT_NUMBER__ */

    #if defined(__MMI_PHB_BIRTHDAY_FIELD__)
        mmi_phb_bday_read_data_to_buff(store_index);
    #endif

    #if defined(__MMI_IMPS__) || defined(__MMI_PHB_POC_FIELD__) || defined(__MMI_VOIP__)
        mmi_phb_service_read_data_to_buff(store_index);
    #endif /* defined(__MMI_IMPS__) || defined(__MMI_PHB_POC_FIELD__) || defined(__MMI_VOIP__) */
    
    #if defined(__MMI_PHB_INFO_FIELD__)
        mmi_phb_info_read_data_to_buff(store_index);
    #endif /* defined(__MMI_PHB_INFO_FIELD__) */
    }

    mmi_phb_populate_save_contact_data_to_buff();
    if (!mmi_phb_op_check_pre_save(TRUE))
    {
        return;
    }
    g_phb_cntx.active_index = mmi_phb_nlf_get_sorted_index(g_phb_cntx.active_index, g_phb_save_contact_opt_storage);
    /* set for op_edit_entry_req */
    mmi_phb_op_edit_entry_req();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_save_contact_opt_update_contact_confirm
 * DESCRIPTION
 *  update contact for unsaved data
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_save_contact_opt_update_contact_confirm(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_display_popup_confirm(
        (UI_string_type) GetString(STR_GLOBAL_YES),
        (U8*) GetImage(IMG_GLOBAL_YES), 
        (UI_string_type) GetString(STR_GLOBAL_NO),
        (U8*) GetImage(IMG_GLOBAL_NO), 
        (UI_string_type) GetString(STR_ID_SAVE_CONTACT_REPLACE_QUERY),
        MMI_EVENT_QUERY);

    SetLeftSoftkeyFunction(mmi_phb_save_contact_opt_update_contact, KEY_EVENT_UP);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_save_contact_opt_select_field
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_save_contact_opt_select_field(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_entry_choose_field(
        g_phb_name_index[mmi_phb_nlf_get_sorted_index(g_phb_cntx.active_index, g_phb_save_contact_opt_storage)],
        MMI_PHB_ENTRY_FIELD_GSM_NUM,
        mmi_phb_entry_save_contact_opt_select_field,
        mmi_phb_entry_save_contact_opt_update_contact_confirm,
        GoBackHistory,
        NULL,
        MMI_TRUE);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_exit_update_list
 * DESCRIPTION
 *  exit function for update list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_exit_save_contact_opt_update_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.end_scr_id = SCR_ID_PHB_SAVE_CONTACT_OPT_UPDATE_LIST;
    wgui_reset_multitap();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_update_list
 * DESCRIPTION
 *  entry function for update list
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_save_contact_opt_update_list(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 *guiBuffer;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_DUAL_SIM_MASTER__
    if (MTPNP_PFAL_Phb_IsReady() == MTPNP_FALSE)
#else   /* __MMI_DUAL_SIM_MASTER__ */
    if (!g_phb_cntx.phb_ready || g_phb_cntx.processing)
#endif  /* __MMI_DUAL_SIM_MASTER__ */
    {
        mmi_phb_entry_not_ready(STR_PROCESSING_PHONEBOOK);
    }
#ifdef __MMI_PHB_USIM_FIELD__
    else if (g_phb_cntx.is_usim && g_phb_cntx.usim_ready_stage != MMI_PHB_USIM_READY)
    {
        mmi_phb_entry_not_ready(STR_ID_PHB_PROCESSING_USIM);
    }
#endif /* __MMI_PHB_USIM_FIELD__ */
#ifdef __SYNCML_SUPPORT__
    else if (mmi_syncml_is_phb_sync_now())
    {
        mmi_phb_entry_not_ready(STR_ID_SYNC_PLEASE_WAIT);
    }
#endif /* __SYNCML_SUPPORT__ */
    else if (mmi_phb_num_of_phb_contact_in_storage(g_phb_save_contact_opt_storage) == 0)
    {
        mmi_display_popup((UI_string_type)GetString(STR_NO_ENTRIES_MESSAGE), MMI_EVENT_FAILURE);
        return;
    }
    else
    {
        EntryNewScreen(SCR_ID_PHB_SAVE_CONTACT_OPT_UPDATE_LIST, mmi_phb_exit_save_contact_opt_update_list, mmi_phb_entry_save_contact_opt_update_list, MMI_FULL_SCREEN);
        guiBuffer = GetCurrGuiBuffer(SCR_ID_PHB_SAVE_CONTACT_OPT_UPDATE_LIST);

        /* bulid alpha_index function. */
        mmi_phb_list_build_alpha_index(NULL, 0, TRUE, g_phb_save_contact_opt_storage);

        if (guiBuffer == NULL)
        {
            g_phb_cntx.highlight_entry = 0;
        }

        RegisterHighlightHandler(mmi_phb_get_index);
        g_phb_nlf_list_storage = g_phb_save_contact_opt_storage;

        ShowCategory184Screen(
            STR_SCR_PBOOK_VIEW_CAPTION,
            IMG_SCR_PBOOK_CAPTION,
            STR_GLOBAL_OK,
            IMG_GLOBAL_OK,
            STR_GLOBAL_BACK,
            IMG_GLOBAL_BACK,
            mmi_phb_num_of_phb_contact_in_storage(g_phb_save_contact_opt_storage),
            mmi_phb_nlf_list_get_item,
            mmi_phb_nlf_list_get_hint,
            g_phb_cntx.highlight_entry,
            guiBuffer);

        SetLeftSoftkeyFunction(mmi_phb_entry_save_contact_opt_select_field, KEY_EVENT_UP);
        SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);

        /* register multitap key for fast jump by alpha index */
#ifdef __MMI_QWERTY_KEYPAD_SUPPORT__
        mmi_frm_set_keypad_mode(KEYPAD_1KEY_QWERTY);
        mmi_frm_set_app_translate_key_handler(mmi_phb_list_alpha_callback);
#else
        wgui_setup_multitap_functions(WGUI_MULTITAP_TYPE_UPPERCASE_ABC, mmi_phb_list_alpha_index, mmi_phb_list_alpha_index_input_callback);
#endif

        SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_save_contact_opt_update
 * DESCRIPTION
 *  save contact opt add to enter replace some duplicated field in phb
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_save_contact_opt_update(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_clear_old_history();
    mmi_phb_entry_save_contact_opt_update_list();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_highlight_unsaved_data_opt_add
 * DESCRIPTION
 *  highlight function for unsaved data option add
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_highlight_save_contact_opt_add(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SetLeftSoftkeyFunction(mmi_phb_save_contact_opt_add, KEY_EVENT_UP);
    SetKeyHandler(mmi_phb_save_contact_opt_add, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_highlight_unsaved_data_opt_update
 * DESCRIPTION
 *  highlight function for unsaved data option update
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_highlight_save_contact_opt_update(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SetLeftSoftkeyFunction(mmi_phb_save_contact_opt_update, KEY_EVENT_UP);
    SetKeyHandler(mmi_phb_save_contact_opt_update, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_save_contact_build_data
 * DESCRIPTION
 *  build phb data in global context
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_save_contact_build_data(MMI_PHB_FIELDS_STRUCT* phb_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 field_len;
    U8 number_UCS2[(MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH];

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_save_contact_data = OslMalloc(sizeof(MMI_PHB_FIELDS_STRUCT));
    memset(g_phb_save_contact_data, 0x00, sizeof(MMI_PHB_FIELDS_STRUCT));

    if (phb_data->name != NULL && (field_len = mmi_ucs2strlen(phb_data->name)))
    {
        g_phb_save_contact_data->name = OslMalloc((MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH);
        memset(g_phb_save_contact_data->name, 0x00, (MAX_PB_NAME_LENGTH + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(g_phb_save_contact_data->name, phb_data->name, (field_len < MAX_PB_NAME_LENGTH ? field_len : MAX_PB_NAME_LENGTH));
    }

    if (phb_data->number != NULL && (field_len = mmi_ucs2strlen(phb_data->number)))
    {            
        memset(number_UCS2, 0, (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
        mmi_phb_truncate_number_field_to_max_len(number_UCS2, (U8*) phb_data->number);

        g_phb_save_contact_data->number = OslMalloc((MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
        memset(g_phb_save_contact_data->number, 0x00, (MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(g_phb_save_contact_data->number, (S8*) number_UCS2, MAX_PB_NUMBER_LENGTH + 1);
    }

#if defined(__MMI_PHB_INFO_FIELD__)
    if (phb_data->pobox != NULL && (field_len = mmi_ucs2strlen(phb_data->pobox)))
    {
        g_phb_save_contact_data->pobox = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        memset(g_phb_save_contact_data->pobox, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(g_phb_save_contact_data->pobox, phb_data->pobox, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
    }

    if (phb_data->extension != NULL && (field_len = mmi_ucs2strlen(phb_data->extension)))
    {
        g_phb_save_contact_data->extension = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        memset(g_phb_save_contact_data->extension, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(g_phb_save_contact_data->extension, phb_data->extension, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
    }

    if (phb_data->street != NULL && (field_len = mmi_ucs2strlen(phb_data->street)))
    {
        g_phb_save_contact_data->street = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        memset(g_phb_save_contact_data->street, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(g_phb_save_contact_data->street, phb_data->street, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
    }

    if (phb_data->city != NULL && (field_len = mmi_ucs2strlen(phb_data->city)))
    {
        g_phb_save_contact_data->city = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        memset(g_phb_save_contact_data->city, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(g_phb_save_contact_data->city, phb_data->city, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
    }

    if (phb_data->state != NULL && (field_len = mmi_ucs2strlen(phb_data->state)))
    {
        g_phb_save_contact_data->state = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        memset(g_phb_save_contact_data->state, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(g_phb_save_contact_data->state, phb_data->state, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
    }

    if (phb_data->postalcode != NULL && (field_len = mmi_ucs2strlen(phb_data->postalcode)))
    {
        g_phb_save_contact_data->postalcode = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        memset(g_phb_save_contact_data->postalcode, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(g_phb_save_contact_data->postalcode, phb_data->postalcode, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
    }

    if (phb_data->country != NULL && (field_len = mmi_ucs2strlen(phb_data->country)))
    {
        g_phb_save_contact_data->country = OslMalloc((MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        memset(g_phb_save_contact_data->country, 0x00, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
        mmi_ucs2ncpy(g_phb_save_contact_data->country, phb_data->country, (field_len < MAX_PB_ADDRESS_LENGTH ? field_len : MAX_PB_ADDRESS_LENGTH));
    }
#endif /* defined(__MMI_PHB_INFO_FIELD__) */
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_save_contact_release_data
 * DESCRIPTION
 *  release phb data buffer
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_save_contact_release_data(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (g_phb_save_contact_data != NULL)
    {
        if (g_phb_save_contact_data->name != NULL)
        {
            OslMfree(g_phb_save_contact_data->name);
        }
    
        if (g_phb_save_contact_data->number != NULL)
        {
            OslMfree(g_phb_save_contact_data->number);
        }

    #if defined(__MMI_PHB_INFO_FIELD__)
        if (g_phb_save_contact_data->pobox != NULL)
        {
            OslMfree(g_phb_save_contact_data->pobox);
        }

        if (g_phb_save_contact_data->extension != NULL)
        {
            OslMfree(g_phb_save_contact_data->extension);
        }

        if (g_phb_save_contact_data->street != NULL)
        {
            OslMfree(g_phb_save_contact_data->street);
        }

        if (g_phb_save_contact_data->city != NULL)
        {
            OslMfree(g_phb_save_contact_data->city);
        }

        if (g_phb_save_contact_data->state != NULL)
        {
            OslMfree(g_phb_save_contact_data->state);
        }

        if (g_phb_save_contact_data->postalcode != NULL)
        {
            OslMfree(g_phb_save_contact_data->postalcode);
        }

        if (g_phb_save_contact_data->country != NULL)
        {
            OslMfree(g_phb_save_contact_data->country);
        }
    #endif /* __MMI_PHB_INFO_FIELD__ */
        OslMfree(g_phb_save_contact_data);
        g_phb_save_contact_data = NULL;
    }
    g_phb_enter_from = MMI_PHB_ENTER_NONE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_general_save_contact_option_delete_cb
 * DESCRIPTION
 *  Delete callback of mmi_phb_entry_general_save_contact_option to delete phb data in global
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
U8 mmi_phb_entry_general_save_contact_option_delete_cb(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_phb_save_contact_release_data();
    g_phb_enter_from = MMI_PHB_ENTER_NONE;

    return MMI_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_exit_general_save_contact_option
 * DESCRIPTION
 *  exit function for mmi_phb_entry_general_save_contact_option
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_exit_general_save_contact_option(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (g_phb_cntx.start_scr_id == 0)
    {
        g_phb_cntx.start_scr_id = SCR_ID_PHB_SAVE_CONTACT_OPT;
    }
    g_phb_cntx.end_scr_id = SCR_ID_PHB_SAVE_CONTACT_OPT;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_entry_general_save_contact_option
 * DESCRIPTION
 *  entry screen of save contact option
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_entry_general_save_contact_option(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 nStrItemList[MAX_SUB_MENUS];
    U16 nNumofItem;
    U8 *guiBuffer;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    EntryNewScreen(SCR_ID_PHB_SAVE_CONTACT_OPT, mmi_phb_exit_general_save_contact_option, mmi_phb_entry_general_save_contact_option, NULL);
    SetDelScrnIDCallbackHandler(SCR_ID_PHB_SAVE_CONTACT_OPT, (HistoryDelCBPtr)mmi_phb_entry_general_save_contact_option_delete_cb);

    mmi_frm_unhide_menu_item(MENU_ID_GENERAL_SAVE_CONTACT_OPT_ADD);
    mmi_frm_unhide_menu_item(MENU_ID_GENERAL_SAVE_CONTACT_OPT_UPDATE);

    if (mmi_phb_num_of_phb_contact_in_storage(g_phb_save_contact_opt_storage) == 0)
    {
        mmi_frm_hide_menu_item(MENU_ID_GENERAL_SAVE_CONTACT_OPT_UPDATE);
    }
    if (mmi_phb_num_of_phb_contact_in_storage(g_phb_save_contact_opt_storage) == mmi_phb_capacity_of_phb_contact_in_storage(g_phb_save_contact_opt_storage))
    {
        mmi_frm_hide_menu_item(MENU_ID_GENERAL_SAVE_CONTACT_OPT_ADD);
    }

    guiBuffer = GetCurrGuiBuffer(SCR_ID_PHB_SAVE_CONTACT_OPT);
    nNumofItem = GetNumOfChild_Ext(MENU_ID_GENERAL_SAVE_CONTACT_OPT);
    GetSequenceStringIds_Ext(MENU_ID_GENERAL_SAVE_CONTACT_OPT, nStrItemList);
    SetParentHandler(MENU_ID_GENERAL_SAVE_CONTACT_OPT);
    RegisterHighlightHandler(ExecuteCurrHiliteHandler);

    ShowCategory15Screen(
        STR_GLOBAL_OPTIONS,
        GetRootTitleIcon(MAIN_MENU_PHONEBOOK_MENUID),
        STR_GLOBAL_OK,
        IMG_GLOBAL_OK,
        STR_GLOBAL_BACK,
        IMG_GLOBAL_BACK,
        nNumofItem,
        nStrItemList,
        (U16*) gIndexIconsImageList,
        1,
        0,
        guiBuffer);

    SetKeyHandler(GoBackHistory, KEY_LEFT_ARROW, KEY_EVENT_DOWN);
    SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
    return;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_save_contact_from_others
 * DESCRIPTION
 *  save a phb contact from other applications
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_save_contact_from_others(U8 storage, MMI_PHB_FIELDS_STRUCT* phb_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_enter_from = MMI_PHB_ENTER_FROM_SAVE_CONTACT;
    g_phb_save_contact_opt_storage = storage;

    mmi_phb_save_contact_build_data(phb_data);
    mmi_phb_entry_general_save_contact_option();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_save_contact_from_others2
 * DESCRIPTION
 *  save a phb contact from other applications
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_save_contact_from_others2(U8 enter, U8 storage, MMI_PHB_FIELDS_STRUCT* phb_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (enter == 0)
    {
        g_phb_enter_from = MMI_PHB_ENTER_FROM_SAVE_CONTACT;
    }
    else
    {
        g_phb_enter_from = enter;
    }
    g_phb_save_contact_opt_storage = storage;

    mmi_phb_save_contact_build_data(phb_data);
    mmi_phb_entry_general_save_contact_option();
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_screen_present
 * DESCRIPTION
 *  if any phonebook screen exist in history , then reture true, others, reture false
 * PARAMETERS
 *  void
 * RETURNS
 *  MMI_BOOL
 *****************************************************************************/
MMI_BOOL mmi_phb_screen_present(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (IsScreenPresent(g_phb_cntx.start_scr_id)
        || IsScreenPresent(g_phb_cntx.end_scr_id)
        || IsScreenPresent(SCR_ID_PHB_UNSAVED_DATA_CONFIRM))
    {
        return MMI_TRUE;
    }

    if ((GetActiveScreenId() >= SCR_PBOOK_MAIN_MENU) && (GetActiveScreenId() < SCR_ID_PHB_ENUM_TOTAL))
    {
        return MMI_TRUE;
    }

    return MMI_FALSE;
}


#ifdef __MMI_UDX_VCARD_SUPPORT__
mmi_udx_store_data_continue_type phb_udx_store_complete_cb;
mmi_udx_get_data_hdlr_type phb_udx_get_data_cb;

U16 phb_udx_index[MAX_PB_ENTRIES];

/*****************************************************************************
 * FUNCTION
 *  mmi_phb_udx_get_user_info
 * DESCRIPTION
 *  get myCard's name for udx
 * PARAMETERS
 *  user_info           [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_udx_get_user_info(S8 *user_info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S16 pError;
    MMI_PHB_VCARD_STRUCT myCard;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ReadRecord(NVRAM_EF_PHB_VCARD_LID, 1, (void*)&myCard, MMI_PHB_VCARD_RECORD_SIZE, &pError);

    mmi_ucs2ncpy(user_info, (const S8 *) myCard.name, MAX_PB_NAME_LENGTH);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_udx_set_processing
 * DESCRIPTION
 *  when udx is busy with phonebook, set phonebook processing
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_udx_set_processing(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.processing = MMI_TRUE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_udx_reset_processing
 * DESCRIPTION
 *  after udx build or parse vcard, reset phonebook processing
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_udx_reset_processing(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_phb_cntx.processing = MMI_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_udx_select_done
 * DESCRIPTION
 *  select data done function
 * PARAMETERS
 *  count           [IN]
 *  phb_index       [IN]
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_phb_udx_select_done(U16 count, U16 *phb_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (count > 0)
    {
        memset(phb_udx_index, 0xff, MAX_PB_ENTRIES * 2);
        memcpy(phb_udx_index, phb_index, count * 2);
        phb_udx_get_data_cb(phb_udx_index, count);
    }
    else
    {
        mmi_display_popup((UI_string_type)GetString(STR_ID_PHB_NO_CONTACT_SELECTED), MMI_EVENT_FAILURE);         
        return;
        
        /* use popup indication , replace reture empty array */
        /* phb_udx_get_data_cb(NULL, 0); */
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_udx_enter_select_entry
 * DESCRIPTION
 *  enter select entry screen for udx
 * PARAMETERS
 *  get_data_hdlr           [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_udx_enter_select_entry(mmi_udx_get_data_hdlr_type get_data_hdlr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/    
#if defined(__MMI_PHB_GENERIC_MULTI_SELECT__)
    phb_udx_get_data_cb = get_data_hdlr;
    
    mmi_phb_generic_multi_select_list_pre_entry(
        (U8) (MMI_PHB_ENTRY_FIELD_ALL_NUMBER | MMI_PHB_ENTRY_FIELD_NAME),
        0xffff,
        mmi_phb_udx_select_done,
        NULL);
#else
    MMI_ASSERT(0);
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_udx_del_scr
 * DESCRIPTION
 *  select entry done , and then del select screen
 * PARAMETERS
 *  del_scr_type           [IN]
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_udx_del_scr(mmi_udx_del_scr_type_enum del_scr_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/    
    if (del_scr_type == MMI_UDX_DEL_SCR_FROM_UDX)
    {
        DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT);
        DeleteScreenIfPresent(SCR_ID_PHB_GENERIC_MULTI_SELECT_AND_QUICK_SEARCH);
    }
    else if (del_scr_type == MMI_UDX_DEL_SCR_FROM_APP)
    {
        DeleteScreenIfPresent(SCR_ID_PHB_MULTI_OP_PHB_LIST);
        DeleteScreenIfPresent(SCR_ID_PHB_MULTI_OP_OPTION);
    }
    else
    {
        MMI_ASSERT(0);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_udx_get_data
 * DESCRIPTION
 *  select entry done , and then del select screen
 * PARAMETERS
 *  store_index_array           [IN]        store index array
 *  sequence                    [IN]        index
 *  data_send_func              [IN]        data send complete function
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_udx_get_data(void* store_index_array, U16 sequence, mmi_udx_get_data_send_type data_send_func)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    udx_vcard_data_struct entry;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT(phb_udx_index == store_index_array);
    
    memset(&entry, 0, sizeof(udx_vcard_data_struct));
    
    if (phb_udx_index[sequence - 1] == 0xffff)
    {
        MMI_ASSERT(0);
    }
    else
    {    
        U16 store_index = 0;
        S8 number_buff[(MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH];
        S8 number_buff2[(MAX_PB_NUMBER_LENGTH + 1 + 1) * ENCODING_LENGTH];
    #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
        S16 pError;
    #endif

        if (!mmi_phb_util_check_entry_exist(phb_udx_index[sequence - 1]))
        {    
            data_send_func(NULL, sequence);
            return;
        }
        else
        {    
            store_index = phb_udx_index[sequence - 1];
        }

        if (((PhoneBook[store_index].tel.type & 0x90) == 0x90) && (PhoneBook[store_index].tel.type != 0xFF))
        {
            mmi_phb_convert_to_digit_by_store_index((U8*) number_buff2, PhoneBook[store_index].tel.number, MAX_PB_NUMBER_LENGTH + 1, store_index);
            /* now number is +**21*8123456# */
            /* call the third API to transfer + to place just after SS pattern */
            applib_decode_number((U8*)number_buff2, (U8*)number_buff, 0x91);
            /* now number is **21*+8123456#, so we can show the original data to user */
        }
        else
        {
            mmi_phb_convert_to_digit_by_store_index((U8*) number_buff, PhoneBook[store_index].tel.number, MAX_PB_NUMBER_LENGTH + 1, store_index);
        }

        mmi_ucs2cpy((S8 *) entry.n, (const S8 *) PhoneBook[store_index].alpha_id.name);
        strncpy((S8 *) entry.tel_cell, (S8 *) number_buff, MAX_PB_NUMBER_LENGTH + 1);
        
        if (store_index < MAX_PB_PHONE_ENTRIES) /* In NVRAM */
        {
        #if defined(__MMI_PHB_BIRTHDAY_FIELD__)
            S8 b_day[9];    /* Format: yyyymmdd */
            mmi_vcard_get_bday_to_buff(store_index, (S8*) b_day);
            strcpy((S8 *) entry.bday, (const S8 *) b_day);
        #endif
        #if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__)
            ReadRecord(
                NVRAM_EF_PHB_FIELDS_LID,
                (U16) (store_index + 1),
                (void*)&PhoneBookOptionalFields,
                OPTIONAL_FIELDS_RECORD_SIZE,
                &pError);

            strncpy((S8 *) entry.tel_home, (const S8 *) PhoneBookOptionalFields.homeNumber, MAX_PB_NUMBER_LENGTH + 1);
            strncpy((S8 *) entry.tel_fax, (const S8 *) PhoneBookOptionalFields.faxNumber, MAX_PB_NUMBER_LENGTH + 1);
            strncpy((S8 *) entry.tel_work, (const S8 *) PhoneBookOptionalFields.officeNumber, MAX_PB_NUMBER_LENGTH + 1);
            strncpy((S8 *) entry.email, (const S8 *) PhoneBookOptionalFields.emailAddress, MAX_PB_EMAIL_LENGTH);
        #ifdef __MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__
            strncpy((S8 *) entry.email_home, (const S8 *) PhoneBookOptionalFields.emailAddress2, MAX_PB_EMAIL_LENGTH);
        #endif
            mmi_ucs2ncpy((S8 *) entry.org_name, (const S8 *) PhoneBookOptionalFields.companyName, MAX_PB_COMPANY_LENGTH);
        #endif
        #ifdef __MMI_PHB_INFO_FIELD__
            mmi_phb_info_read_data_to_buff(store_index);
            
            mmi_ucs2ncpy((S8 *) entry.title, (const S8 *) pbTitle, MAX_PB_TITLE_LENGTH);
            mmi_ucs2_n_to_asc((S8 *) entry.url, (S8 *) pbUrl, (MAX_PB_URL_LENGTH * ENCODING_LENGTH));
            mmi_ucs2ncpy((S8 *) entry.adr_city, (const S8 *) pbAdrCity, MAX_PB_ADDRESS_LENGTH);
            mmi_ucs2ncpy((S8 *) entry.adr_country, (const S8 *) pbAdrCountry, MAX_PB_ADDRESS_LENGTH);
            mmi_ucs2ncpy((S8 *) entry.adr_detail, (const S8 *) pbAdrExtension, MAX_PB_ADDRESS_LENGTH);
            mmi_ucs2ncpy((S8 *) entry.adr_mailbox, (const S8 *) pbAdrPOBox, MAX_PB_ADDRESS_LENGTH);
            mmi_ucs2ncpy((S8 *) entry.adr_postcode, (const S8 *) pbAdrPostalCode, MAX_PB_ADDRESS_LENGTH);
            mmi_ucs2ncpy((S8 *) entry.adr_province, (const S8 *) pbAdrState, MAX_PB_ADDRESS_LENGTH);
            mmi_ucs2ncpy((S8 *) entry.adr_street, (const S8 *) pbAdrStreet, MAX_PB_ADDRESS_LENGTH);
        #endif 
        }

        data_send_func(&entry, sequence);
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_phb_udx_store_data
 * DESCRIPTION
 *  store data to phonebook
 * PARAMETERS
 *  vcard_data              [IN]        data struct
 *  store_complete_func     [IN]        after store data, call complete cb
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_phb_udx_store_data(void* vcard_data, mmi_udx_store_data_continue_type store_complete_func)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    udx_vcard_data_struct *entry_ptr = (udx_vcard_data_struct *) vcard_data;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    phb_udx_store_complete_cb = store_complete_func;

    if (g_phb_cntx.phone_used < g_phb_cntx.phone_total)
    {
        g_phb_cntx.selected_storage = MMI_NVRAM;
    }
    else
    {
        phb_udx_store_complete_cb(MMI_TRUE, MMI_FALSE);
        return;
    }   

    /*
     * Prepare data for name and number field
     */
    mmi_phb_util_clear_buffer(TRUE);

    if (mmi_ucs2strlen((const S8 *) entry_ptr->n))
    {
        mmi_ucs2ncpy(pbName, (S8 *) entry_ptr->n, MAX_PB_NAME_LENGTH);            
        mmi_phb_truncate_pbname_buffer(MMI_NVRAM);
    }

    mmi_asc_n_to_ucs2((S8 *) pbNumber, (S8 *) entry_ptr->tel_cell, (U32) (MAX_PB_NUMBER_LENGTH + 1));

    /*
     * Prepare data for optional fields
     */
#if !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) || defined(__MMI_PHB_USIM_FIELD__)
    mmi_asc_n_to_ucs2((S8 *) pbHomeNumber, (S8 *) entry_ptr->tel_home, (U32) (MAX_PB_NUMBER_LENGTH + 1));
    mmi_asc_n_to_ucs2((S8 *) pbOfficeNumber, (S8 *) entry_ptr->tel_work, (U32) (MAX_PB_NUMBER_LENGTH + 1));
    mmi_asc_n_to_ucs2((S8 *) pbFaxNumber, (S8 *) entry_ptr->tel_fax, (U32) (MAX_PB_NUMBER_LENGTH + 1));
    mmi_asc_n_to_ucs2((S8 *) pbEmailAddress, (S8 *) entry_ptr->email, (U32) MAX_PB_EMAIL_LENGTH);
#ifdef __MMI_PHB_OPTIONAL_FIELDS_2ND_EMAIL__
    mmi_asc_n_to_ucs2((S8 *) pbEmailAddress2, (S8 *) entry_ptr->email_home, (U32) MAX_PB_EMAIL_LENGTH);
#endif
    memcpy(pbCompanyName, entry_ptr->org_name, (MAX_PB_COMPANY_LENGTH + 1) * ENCODING_LENGTH);
#endif /* !defined(__MMI_PHB_NO_OPTIONAL_FIELD__) || defined(__MMI_PHB_USIM_FIELD__) */        

    mmi_phb_entry_op_remove_invalid_name_and_number();
    /*
     * Prepare data for birthday
     */
#ifdef __MMI_PHB_BIRTHDAY_FIELD__
    if (strlen((S8*)entry_ptr->bday))
    {
        mmi_phb_retrieve_bday((S8*)entry_ptr->bday, (S8*) pbYear, (S8*) pbMon, (S8*) pbDay, TRUE);
    }
#endif /* __MMI_PHB_BIRTHDAY_FIELD__ */ 

#if defined(__MMI_PHB_INFO_FIELD__)
    memcpy(pbTitle, entry_ptr->title, (MAX_PB_TITLE_LENGTH + 1) * ENCODING_LENGTH);
    mmi_asc_n_to_ucs2((S8 *) pbUrl, (S8 *) entry_ptr->url, (U32) MAX_PB_URL_LENGTH);
    memcpy(pbAdrPOBox, entry_ptr->adr_mailbox, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
    memcpy(pbAdrExtension, entry_ptr->adr_detail, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
    memcpy(pbAdrStreet, entry_ptr->adr_street, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
    memcpy(pbAdrCity, entry_ptr->adr_city, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
    memcpy(pbAdrState, entry_ptr->adr_province, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
    memcpy(pbAdrPostalCode, entry_ptr->adr_postcode, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
    memcpy(pbAdrCountry, entry_ptr->adr_country, (MAX_PB_ADDRESS_LENGTH + 1) * ENCODING_LENGTH);
    //memcpy(pbNote, entry_ptr->, (MAX_PB_NOTE_LENGTH + 1) * ENCODING_LENGTH);
#endif /* defined(__MMI_PHB_INFO_FIELD__) */
    /*
     * prepare data for optional id
     */

    g_phb_cntx.image_location = MMI_PHB_IMAGE_NO_SELECT;
    g_phb_cntx.selected_pic_index = 0;
    g_phb_cntx.selected_ring_index = 0;
    g_phb_cntx.selected_grp_index = 0;

    g_phb_enter_from = MMI_PHB_ENTER_FROM_UDX;

    /* for check pre save */
    if (!mmi_phb_op_check_pre_save(MMI_FALSE))
    {
        phb_udx_store_complete_cb(MMI_FALSE, MMI_FALSE);
        return;
    }

    /* need to handle USIM here */
    mmi_phb_op_add_entry_req();
}
#endif

#endif /* _PHONEBOOKSTUBSTOOTHERS_C */ 

